validation.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. // Copyright 2015 xeipuuv ( https://github.com/xeipuuv )
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // author xeipuuv
  15. // author-github https://github.com/xeipuuv
  16. // author-mail [email protected]
  17. //
  18. // repository-name gojsonschema
  19. // repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language.
  20. //
  21. // description Extends Schema and subSchema, implements the validation phase.
  22. //
  23. // created 28-02-2013
  24. package gojsonschema
  25. import (
  26. "encoding/json"
  27. "reflect"
  28. "regexp"
  29. "strconv"
  30. "strings"
  31. "unicode/utf8"
  32. )
  33. func Validate(ls JSONLoader, ld JSONLoader) (*Result, error) {
  34. var err error
  35. // load schema
  36. schema, err := NewSchema(ls)
  37. if err != nil {
  38. return nil, err
  39. }
  40. // begine validation
  41. return schema.Validate(ld)
  42. }
  43. func (v *Schema) Validate(l JSONLoader) (*Result, error) {
  44. // load document
  45. root, err := l.LoadJSON()
  46. if err != nil {
  47. return nil, err
  48. }
  49. // begin validation
  50. result := &Result{}
  51. context := newJsonContext(STRING_CONTEXT_ROOT, nil)
  52. v.rootSchema.validateRecursive(v.rootSchema, root, result, context)
  53. return result, nil
  54. }
  55. func (v *subSchema) subValidateWithContext(document interface{}, context *jsonContext) *Result {
  56. result := &Result{}
  57. v.validateRecursive(v, document, result, context)
  58. return result
  59. }
  60. // Walker function to validate the json recursively against the subSchema
  61. func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *jsonContext) {
  62. if internalLogEnabled {
  63. internalLog("validateRecursive %s", context.String())
  64. internalLog(" %v", currentNode)
  65. }
  66. // Handle referenced schemas, returns directly when a $ref is found
  67. if currentSubSchema.refSchema != nil {
  68. v.validateRecursive(currentSubSchema.refSchema, currentNode, result, context)
  69. return
  70. }
  71. // Check for null value
  72. if currentNode == nil {
  73. if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_NULL) {
  74. result.addError(
  75. new(InvalidTypeError),
  76. context,
  77. currentNode,
  78. ErrorDetails{
  79. "expected": currentSubSchema.types.String(),
  80. "given": TYPE_NULL,
  81. },
  82. )
  83. return
  84. }
  85. currentSubSchema.validateSchema(currentSubSchema, currentNode, result, context)
  86. v.validateCommon(currentSubSchema, currentNode, result, context)
  87. } else { // Not a null value
  88. if isJsonNumber(currentNode) {
  89. value := currentNode.(json.Number)
  90. _, isValidInt64, _ := checkJsonNumber(value)
  91. validType := currentSubSchema.types.Contains(TYPE_NUMBER) || (isValidInt64 && currentSubSchema.types.Contains(TYPE_INTEGER))
  92. if currentSubSchema.types.IsTyped() && !validType {
  93. givenType := TYPE_INTEGER
  94. if !isValidInt64 {
  95. givenType = TYPE_NUMBER
  96. }
  97. result.addError(
  98. new(InvalidTypeError),
  99. context,
  100. currentNode,
  101. ErrorDetails{
  102. "expected": currentSubSchema.types.String(),
  103. "given": givenType,
  104. },
  105. )
  106. return
  107. }
  108. currentSubSchema.validateSchema(currentSubSchema, value, result, context)
  109. v.validateNumber(currentSubSchema, value, result, context)
  110. v.validateCommon(currentSubSchema, value, result, context)
  111. v.validateString(currentSubSchema, value, result, context)
  112. } else {
  113. rValue := reflect.ValueOf(currentNode)
  114. rKind := rValue.Kind()
  115. switch rKind {
  116. // Slice => JSON array
  117. case reflect.Slice:
  118. if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_ARRAY) {
  119. result.addError(
  120. new(InvalidTypeError),
  121. context,
  122. currentNode,
  123. ErrorDetails{
  124. "expected": currentSubSchema.types.String(),
  125. "given": TYPE_ARRAY,
  126. },
  127. )
  128. return
  129. }
  130. castCurrentNode := currentNode.([]interface{})
  131. currentSubSchema.validateSchema(currentSubSchema, castCurrentNode, result, context)
  132. v.validateArray(currentSubSchema, castCurrentNode, result, context)
  133. v.validateCommon(currentSubSchema, castCurrentNode, result, context)
  134. // Map => JSON object
  135. case reflect.Map:
  136. if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_OBJECT) {
  137. result.addError(
  138. new(InvalidTypeError),
  139. context,
  140. currentNode,
  141. ErrorDetails{
  142. "expected": currentSubSchema.types.String(),
  143. "given": TYPE_OBJECT,
  144. },
  145. )
  146. return
  147. }
  148. castCurrentNode, ok := currentNode.(map[string]interface{})
  149. if !ok {
  150. castCurrentNode = convertDocumentNode(currentNode).(map[string]interface{})
  151. }
  152. currentSubSchema.validateSchema(currentSubSchema, castCurrentNode, result, context)
  153. v.validateObject(currentSubSchema, castCurrentNode, result, context)
  154. v.validateCommon(currentSubSchema, castCurrentNode, result, context)
  155. for _, pSchema := range currentSubSchema.propertiesChildren {
  156. nextNode, ok := castCurrentNode[pSchema.property]
  157. if ok {
  158. subContext := newJsonContext(pSchema.property, context)
  159. v.validateRecursive(pSchema, nextNode, result, subContext)
  160. }
  161. }
  162. // Simple JSON values : string, number, boolean
  163. case reflect.Bool:
  164. if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_BOOLEAN) {
  165. result.addError(
  166. new(InvalidTypeError),
  167. context,
  168. currentNode,
  169. ErrorDetails{
  170. "expected": currentSubSchema.types.String(),
  171. "given": TYPE_BOOLEAN,
  172. },
  173. )
  174. return
  175. }
  176. value := currentNode.(bool)
  177. currentSubSchema.validateSchema(currentSubSchema, value, result, context)
  178. v.validateNumber(currentSubSchema, value, result, context)
  179. v.validateCommon(currentSubSchema, value, result, context)
  180. v.validateString(currentSubSchema, value, result, context)
  181. case reflect.String:
  182. if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_STRING) {
  183. result.addError(
  184. new(InvalidTypeError),
  185. context,
  186. currentNode,
  187. ErrorDetails{
  188. "expected": currentSubSchema.types.String(),
  189. "given": TYPE_STRING,
  190. },
  191. )
  192. return
  193. }
  194. value := currentNode.(string)
  195. currentSubSchema.validateSchema(currentSubSchema, value, result, context)
  196. v.validateNumber(currentSubSchema, value, result, context)
  197. v.validateCommon(currentSubSchema, value, result, context)
  198. v.validateString(currentSubSchema, value, result, context)
  199. }
  200. }
  201. }
  202. result.incrementScore()
  203. }
  204. // Different kinds of validation there, subSchema / common / array / object / string...
  205. func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *jsonContext) {
  206. if internalLogEnabled {
  207. internalLog("validateSchema %s", context.String())
  208. internalLog(" %v", currentNode)
  209. }
  210. if len(currentSubSchema.anyOf) > 0 {
  211. validatedAnyOf := false
  212. var bestValidationResult *Result
  213. for _, anyOfSchema := range currentSubSchema.anyOf {
  214. if !validatedAnyOf {
  215. validationResult := anyOfSchema.subValidateWithContext(currentNode, context)
  216. validatedAnyOf = validationResult.Valid()
  217. if !validatedAnyOf && (bestValidationResult == nil || validationResult.score > bestValidationResult.score) {
  218. bestValidationResult = validationResult
  219. }
  220. }
  221. }
  222. if !validatedAnyOf {
  223. result.addError(new(NumberAnyOfError), context, currentNode, ErrorDetails{})
  224. if bestValidationResult != nil {
  225. // add error messages of closest matching subSchema as
  226. // that's probably the one the user was trying to match
  227. result.mergeErrors(bestValidationResult)
  228. }
  229. }
  230. }
  231. if len(currentSubSchema.oneOf) > 0 {
  232. nbValidated := 0
  233. var bestValidationResult *Result
  234. for _, oneOfSchema := range currentSubSchema.oneOf {
  235. validationResult := oneOfSchema.subValidateWithContext(currentNode, context)
  236. if validationResult.Valid() {
  237. nbValidated++
  238. } else if nbValidated == 0 && (bestValidationResult == nil || validationResult.score > bestValidationResult.score) {
  239. bestValidationResult = validationResult
  240. }
  241. }
  242. if nbValidated != 1 {
  243. result.addError(new(NumberOneOfError), context, currentNode, ErrorDetails{})
  244. if nbValidated == 0 {
  245. // add error messages of closest matching subSchema as
  246. // that's probably the one the user was trying to match
  247. result.mergeErrors(bestValidationResult)
  248. }
  249. }
  250. }
  251. if len(currentSubSchema.allOf) > 0 {
  252. nbValidated := 0
  253. for _, allOfSchema := range currentSubSchema.allOf {
  254. validationResult := allOfSchema.subValidateWithContext(currentNode, context)
  255. if validationResult.Valid() {
  256. nbValidated++
  257. }
  258. result.mergeErrors(validationResult)
  259. }
  260. if nbValidated != len(currentSubSchema.allOf) {
  261. result.addError(new(NumberAllOfError), context, currentNode, ErrorDetails{})
  262. }
  263. }
  264. if currentSubSchema.not != nil {
  265. validationResult := currentSubSchema.not.subValidateWithContext(currentNode, context)
  266. if validationResult.Valid() {
  267. result.addError(new(NumberNotError), context, currentNode, ErrorDetails{})
  268. }
  269. }
  270. if currentSubSchema.dependencies != nil && len(currentSubSchema.dependencies) > 0 {
  271. if isKind(currentNode, reflect.Map) {
  272. for elementKey := range currentNode.(map[string]interface{}) {
  273. if dependency, ok := currentSubSchema.dependencies[elementKey]; ok {
  274. switch dependency := dependency.(type) {
  275. case []string:
  276. for _, dependOnKey := range dependency {
  277. if _, dependencyResolved := currentNode.(map[string]interface{})[dependOnKey]; !dependencyResolved {
  278. result.addError(
  279. new(MissingDependencyError),
  280. context,
  281. currentNode,
  282. ErrorDetails{"dependency": dependOnKey},
  283. )
  284. }
  285. }
  286. case *subSchema:
  287. dependency.validateRecursive(dependency, currentNode, result, context)
  288. }
  289. }
  290. }
  291. }
  292. }
  293. result.incrementScore()
  294. }
  295. func (v *subSchema) validateCommon(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {
  296. if internalLogEnabled {
  297. internalLog("validateCommon %s", context.String())
  298. internalLog(" %v", value)
  299. }
  300. // enum:
  301. if len(currentSubSchema.enum) > 0 {
  302. has, err := currentSubSchema.ContainsEnum(value)
  303. if err != nil {
  304. result.addError(new(InternalError), context, value, ErrorDetails{"error": err})
  305. }
  306. if !has {
  307. result.addError(
  308. new(EnumError),
  309. context,
  310. value,
  311. ErrorDetails{
  312. "allowed": strings.Join(currentSubSchema.enum, ", "),
  313. },
  314. )
  315. }
  316. }
  317. result.incrementScore()
  318. }
  319. func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface{}, result *Result, context *jsonContext) {
  320. if internalLogEnabled {
  321. internalLog("validateArray %s", context.String())
  322. internalLog(" %v", value)
  323. }
  324. nbValues := len(value)
  325. // TODO explain
  326. if currentSubSchema.itemsChildrenIsSingleSchema {
  327. for i := range value {
  328. subContext := newJsonContext(strconv.Itoa(i), context)
  329. validationResult := currentSubSchema.itemsChildren[0].subValidateWithContext(value[i], subContext)
  330. result.mergeErrors(validationResult)
  331. }
  332. } else {
  333. if currentSubSchema.itemsChildren != nil && len(currentSubSchema.itemsChildren) > 0 {
  334. nbItems := len(currentSubSchema.itemsChildren)
  335. // while we have both schemas and values, check them against each other
  336. for i := 0; i != nbItems && i != nbValues; i++ {
  337. subContext := newJsonContext(strconv.Itoa(i), context)
  338. validationResult := currentSubSchema.itemsChildren[i].subValidateWithContext(value[i], subContext)
  339. result.mergeErrors(validationResult)
  340. }
  341. if nbItems < nbValues {
  342. // we have less schemas than elements in the instance array,
  343. // but that might be ok if "additionalItems" is specified.
  344. switch currentSubSchema.additionalItems.(type) {
  345. case bool:
  346. if !currentSubSchema.additionalItems.(bool) {
  347. result.addError(new(ArrayNoAdditionalItemsError), context, value, ErrorDetails{})
  348. }
  349. case *subSchema:
  350. additionalItemSchema := currentSubSchema.additionalItems.(*subSchema)
  351. for i := nbItems; i != nbValues; i++ {
  352. subContext := newJsonContext(strconv.Itoa(i), context)
  353. validationResult := additionalItemSchema.subValidateWithContext(value[i], subContext)
  354. result.mergeErrors(validationResult)
  355. }
  356. }
  357. }
  358. }
  359. }
  360. // minItems & maxItems
  361. if currentSubSchema.minItems != nil {
  362. if nbValues < int(*currentSubSchema.minItems) {
  363. result.addError(
  364. new(ArrayMinItemsError),
  365. context,
  366. value,
  367. ErrorDetails{"min": *currentSubSchema.minItems},
  368. )
  369. }
  370. }
  371. if currentSubSchema.maxItems != nil {
  372. if nbValues > int(*currentSubSchema.maxItems) {
  373. result.addError(
  374. new(ArrayMaxItemsError),
  375. context,
  376. value,
  377. ErrorDetails{"max": *currentSubSchema.maxItems},
  378. )
  379. }
  380. }
  381. // uniqueItems:
  382. if currentSubSchema.uniqueItems {
  383. var stringifiedItems []string
  384. for _, v := range value {
  385. vString, err := marshalToJsonString(v)
  386. if err != nil {
  387. result.addError(new(InternalError), context, value, ErrorDetails{"err": err})
  388. }
  389. if isStringInSlice(stringifiedItems, *vString) {
  390. result.addError(
  391. new(ItemsMustBeUniqueError),
  392. context,
  393. value,
  394. ErrorDetails{"type": TYPE_ARRAY},
  395. )
  396. }
  397. stringifiedItems = append(stringifiedItems, *vString)
  398. }
  399. }
  400. result.incrementScore()
  401. }
  402. func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string]interface{}, result *Result, context *jsonContext) {
  403. if internalLogEnabled {
  404. internalLog("validateObject %s", context.String())
  405. internalLog(" %v", value)
  406. }
  407. // minProperties & maxProperties:
  408. if currentSubSchema.minProperties != nil {
  409. if len(value) < int(*currentSubSchema.minProperties) {
  410. result.addError(
  411. new(ArrayMinPropertiesError),
  412. context,
  413. value,
  414. ErrorDetails{"min": *currentSubSchema.minProperties},
  415. )
  416. }
  417. }
  418. if currentSubSchema.maxProperties != nil {
  419. if len(value) > int(*currentSubSchema.maxProperties) {
  420. result.addError(
  421. new(ArrayMaxPropertiesError),
  422. context,
  423. value,
  424. ErrorDetails{"max": *currentSubSchema.maxProperties},
  425. )
  426. }
  427. }
  428. // required:
  429. for _, requiredProperty := range currentSubSchema.required {
  430. _, ok := value[requiredProperty]
  431. if ok {
  432. result.incrementScore()
  433. } else {
  434. result.addError(
  435. new(RequiredError),
  436. context,
  437. value,
  438. ErrorDetails{"property": requiredProperty},
  439. )
  440. }
  441. }
  442. // additionalProperty & patternProperty:
  443. if currentSubSchema.additionalProperties != nil {
  444. switch currentSubSchema.additionalProperties.(type) {
  445. case bool:
  446. if !currentSubSchema.additionalProperties.(bool) {
  447. for pk := range value {
  448. found := false
  449. for _, spValue := range currentSubSchema.propertiesChildren {
  450. if pk == spValue.property {
  451. found = true
  452. }
  453. }
  454. pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
  455. if found {
  456. if pp_has && !pp_match {
  457. result.addError(
  458. new(AdditionalPropertyNotAllowedError),
  459. context,
  460. value[pk],
  461. ErrorDetails{"property": pk},
  462. )
  463. }
  464. } else {
  465. if !pp_has || !pp_match {
  466. result.addError(
  467. new(AdditionalPropertyNotAllowedError),
  468. context,
  469. value[pk],
  470. ErrorDetails{"property": pk},
  471. )
  472. }
  473. }
  474. }
  475. }
  476. case *subSchema:
  477. additionalPropertiesSchema := currentSubSchema.additionalProperties.(*subSchema)
  478. for pk := range value {
  479. found := false
  480. for _, spValue := range currentSubSchema.propertiesChildren {
  481. if pk == spValue.property {
  482. found = true
  483. }
  484. }
  485. pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
  486. if found {
  487. if pp_has && !pp_match {
  488. validationResult := additionalPropertiesSchema.subValidateWithContext(value[pk], context)
  489. result.mergeErrors(validationResult)
  490. }
  491. } else {
  492. if !pp_has || !pp_match {
  493. validationResult := additionalPropertiesSchema.subValidateWithContext(value[pk], context)
  494. result.mergeErrors(validationResult)
  495. }
  496. }
  497. }
  498. }
  499. } else {
  500. for pk := range value {
  501. pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
  502. if pp_has && !pp_match {
  503. result.addError(
  504. new(InvalidPropertyPatternError),
  505. context,
  506. value[pk],
  507. ErrorDetails{
  508. "property": pk,
  509. "pattern": currentSubSchema.PatternPropertiesString(),
  510. },
  511. )
  512. }
  513. }
  514. }
  515. result.incrementScore()
  516. }
  517. func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key string, value interface{}, result *Result, context *jsonContext) (has bool, matched bool) {
  518. if internalLogEnabled {
  519. internalLog("validatePatternProperty %s", context.String())
  520. internalLog(" %s %v", key, value)
  521. }
  522. has = false
  523. validatedkey := false
  524. for pk, pv := range currentSubSchema.patternProperties {
  525. if matches, _ := regexp.MatchString(pk, key); matches {
  526. has = true
  527. subContext := newJsonContext(key, context)
  528. validationResult := pv.subValidateWithContext(value, subContext)
  529. result.mergeErrors(validationResult)
  530. if validationResult.Valid() {
  531. validatedkey = true
  532. }
  533. }
  534. }
  535. if !validatedkey {
  536. return has, false
  537. }
  538. result.incrementScore()
  539. return has, true
  540. }
  541. func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {
  542. // Ignore JSON numbers
  543. if isJsonNumber(value) {
  544. return
  545. }
  546. // Ignore non strings
  547. if !isKind(value, reflect.String) {
  548. return
  549. }
  550. if internalLogEnabled {
  551. internalLog("validateString %s", context.String())
  552. internalLog(" %v", value)
  553. }
  554. stringValue := value.(string)
  555. // minLength & maxLength:
  556. if currentSubSchema.minLength != nil {
  557. if utf8.RuneCount([]byte(stringValue)) < int(*currentSubSchema.minLength) {
  558. result.addError(
  559. new(StringLengthGTEError),
  560. context,
  561. value,
  562. ErrorDetails{"min": *currentSubSchema.minLength},
  563. )
  564. }
  565. }
  566. if currentSubSchema.maxLength != nil {
  567. if utf8.RuneCount([]byte(stringValue)) > int(*currentSubSchema.maxLength) {
  568. result.addError(
  569. new(StringLengthLTEError),
  570. context,
  571. value,
  572. ErrorDetails{"max": *currentSubSchema.maxLength},
  573. )
  574. }
  575. }
  576. // pattern:
  577. if currentSubSchema.pattern != nil {
  578. if !currentSubSchema.pattern.MatchString(stringValue) {
  579. result.addError(
  580. new(DoesNotMatchPatternError),
  581. context,
  582. value,
  583. ErrorDetails{"pattern": currentSubSchema.pattern},
  584. )
  585. }
  586. }
  587. // format
  588. if currentSubSchema.format != "" {
  589. if !FormatCheckers.IsFormat(currentSubSchema.format, stringValue) {
  590. result.addError(
  591. new(DoesNotMatchFormatError),
  592. context,
  593. value,
  594. ErrorDetails{"format": currentSubSchema.format},
  595. )
  596. }
  597. }
  598. result.incrementScore()
  599. }
  600. func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {
  601. // Ignore non numbers
  602. if !isJsonNumber(value) {
  603. return
  604. }
  605. if internalLogEnabled {
  606. internalLog("validateNumber %s", context.String())
  607. internalLog(" %v", value)
  608. }
  609. number := value.(json.Number)
  610. float64Value, _ := number.Float64()
  611. // multipleOf:
  612. if currentSubSchema.multipleOf != nil {
  613. if !isFloat64AnInteger(float64Value / *currentSubSchema.multipleOf) {
  614. result.addError(
  615. new(MultipleOfError),
  616. context,
  617. resultErrorFormatJsonNumber(number),
  618. ErrorDetails{"multiple": *currentSubSchema.multipleOf},
  619. )
  620. }
  621. }
  622. //maximum & exclusiveMaximum:
  623. if currentSubSchema.maximum != nil {
  624. if currentSubSchema.exclusiveMaximum {
  625. if float64Value >= *currentSubSchema.maximum {
  626. result.addError(
  627. new(NumberLTError),
  628. context,
  629. resultErrorFormatJsonNumber(number),
  630. ErrorDetails{
  631. "max": resultErrorFormatNumber(*currentSubSchema.maximum),
  632. },
  633. )
  634. }
  635. } else {
  636. if float64Value > *currentSubSchema.maximum {
  637. result.addError(
  638. new(NumberLTEError),
  639. context,
  640. resultErrorFormatJsonNumber(number),
  641. ErrorDetails{
  642. "max": resultErrorFormatNumber(*currentSubSchema.maximum),
  643. },
  644. )
  645. }
  646. }
  647. }
  648. //minimum & exclusiveMinimum:
  649. if currentSubSchema.minimum != nil {
  650. if currentSubSchema.exclusiveMinimum {
  651. if float64Value <= *currentSubSchema.minimum {
  652. result.addError(
  653. new(NumberGTError),
  654. context,
  655. resultErrorFormatJsonNumber(number),
  656. ErrorDetails{
  657. "min": resultErrorFormatNumber(*currentSubSchema.minimum),
  658. },
  659. )
  660. }
  661. } else {
  662. if float64Value < *currentSubSchema.minimum {
  663. result.addError(
  664. new(NumberGTEError),
  665. context,
  666. resultErrorFormatJsonNumber(number),
  667. ErrorDetails{
  668. "min": resultErrorFormatNumber(*currentSubSchema.minimum),
  669. },
  670. )
  671. }
  672. }
  673. }
  674. result.incrementScore()
  675. }