validation.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. nbItems := 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. nbValues := len(value)
  336. if nbItems == nbValues {
  337. for i := 0; i != nbItems; i++ {
  338. subContext := newJsonContext(strconv.Itoa(i), context)
  339. validationResult := currentSubSchema.itemsChildren[i].subValidateWithContext(value[i], subContext)
  340. result.mergeErrors(validationResult)
  341. }
  342. } else if nbItems < nbValues {
  343. switch currentSubSchema.additionalItems.(type) {
  344. case bool:
  345. if !currentSubSchema.additionalItems.(bool) {
  346. result.addError(new(ArrayNoAdditionalItemsError), context, value, ErrorDetails{})
  347. }
  348. case *subSchema:
  349. additionalItemSchema := currentSubSchema.additionalItems.(*subSchema)
  350. for i := nbItems; i != nbValues; i++ {
  351. subContext := newJsonContext(strconv.Itoa(i), context)
  352. validationResult := additionalItemSchema.subValidateWithContext(value[i], subContext)
  353. result.mergeErrors(validationResult)
  354. }
  355. }
  356. }
  357. }
  358. }
  359. // minItems & maxItems
  360. if currentSubSchema.minItems != nil {
  361. if nbItems < int(*currentSubSchema.minItems) {
  362. result.addError(
  363. new(ArrayMinItemsError),
  364. context,
  365. value,
  366. ErrorDetails{"min": *currentSubSchema.minItems},
  367. )
  368. }
  369. }
  370. if currentSubSchema.maxItems != nil {
  371. if nbItems > int(*currentSubSchema.maxItems) {
  372. result.addError(
  373. new(ArrayMaxItemsError),
  374. context,
  375. value,
  376. ErrorDetails{"max": *currentSubSchema.maxItems},
  377. )
  378. }
  379. }
  380. // uniqueItems:
  381. if currentSubSchema.uniqueItems {
  382. var stringifiedItems []string
  383. for _, v := range value {
  384. vString, err := marshalToJsonString(v)
  385. if err != nil {
  386. result.addError(new(InternalError), context, value, ErrorDetails{"err": err})
  387. }
  388. if isStringInSlice(stringifiedItems, *vString) {
  389. result.addError(
  390. new(ItemsMustBeUniqueError),
  391. context,
  392. value,
  393. ErrorDetails{"type": TYPE_ARRAY},
  394. )
  395. }
  396. stringifiedItems = append(stringifiedItems, *vString)
  397. }
  398. }
  399. result.incrementScore()
  400. }
  401. func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string]interface{}, result *Result, context *jsonContext) {
  402. if internalLogEnabled {
  403. internalLog("validateObject %s", context.String())
  404. internalLog(" %v", value)
  405. }
  406. // minProperties & maxProperties:
  407. if currentSubSchema.minProperties != nil {
  408. if len(value) < int(*currentSubSchema.minProperties) {
  409. result.addError(
  410. new(ArrayMinPropertiesError),
  411. context,
  412. value,
  413. ErrorDetails{"min": *currentSubSchema.minProperties},
  414. )
  415. }
  416. }
  417. if currentSubSchema.maxProperties != nil {
  418. if len(value) > int(*currentSubSchema.maxProperties) {
  419. result.addError(
  420. new(ArrayMaxPropertiesError),
  421. context,
  422. value,
  423. ErrorDetails{"max": *currentSubSchema.maxProperties},
  424. )
  425. }
  426. }
  427. // required:
  428. for _, requiredProperty := range currentSubSchema.required {
  429. _, ok := value[requiredProperty]
  430. if ok {
  431. result.incrementScore()
  432. } else {
  433. result.addError(
  434. new(RequiredError),
  435. context,
  436. value,
  437. ErrorDetails{"property": requiredProperty},
  438. )
  439. }
  440. }
  441. // additionalProperty & patternProperty:
  442. if currentSubSchema.additionalProperties != nil {
  443. switch currentSubSchema.additionalProperties.(type) {
  444. case bool:
  445. if !currentSubSchema.additionalProperties.(bool) {
  446. for pk := range value {
  447. found := false
  448. for _, spValue := range currentSubSchema.propertiesChildren {
  449. if pk == spValue.property {
  450. found = true
  451. }
  452. }
  453. pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
  454. if found {
  455. if pp_has && !pp_match {
  456. result.addError(
  457. new(AdditionalPropertyNotAllowedError),
  458. context,
  459. value,
  460. ErrorDetails{"property": pk},
  461. )
  462. }
  463. } else {
  464. if !pp_has || !pp_match {
  465. result.addError(
  466. new(AdditionalPropertyNotAllowedError),
  467. context,
  468. value,
  469. ErrorDetails{"property": pk},
  470. )
  471. }
  472. }
  473. }
  474. }
  475. case *subSchema:
  476. additionalPropertiesSchema := currentSubSchema.additionalProperties.(*subSchema)
  477. for pk := range value {
  478. found := false
  479. for _, spValue := range currentSubSchema.propertiesChildren {
  480. if pk == spValue.property {
  481. found = true
  482. }
  483. }
  484. pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
  485. if found {
  486. if pp_has && !pp_match {
  487. validationResult := additionalPropertiesSchema.subValidateWithContext(value[pk], context)
  488. result.mergeErrors(validationResult)
  489. }
  490. } else {
  491. if !pp_has || !pp_match {
  492. validationResult := additionalPropertiesSchema.subValidateWithContext(value[pk], context)
  493. result.mergeErrors(validationResult)
  494. }
  495. }
  496. }
  497. }
  498. } else {
  499. for pk := range value {
  500. pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
  501. if pp_has && !pp_match {
  502. result.addError(
  503. new(InvalidPropertyPatternError),
  504. context,
  505. value,
  506. ErrorDetails{
  507. "property": pk,
  508. "pattern": currentSubSchema.PatternPropertiesString(),
  509. },
  510. )
  511. }
  512. }
  513. }
  514. result.incrementScore()
  515. }
  516. func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key string, value interface{}, result *Result, context *jsonContext) (has bool, matched bool) {
  517. if internalLogEnabled {
  518. internalLog("validatePatternProperty %s", context.String())
  519. internalLog(" %s %v", key, value)
  520. }
  521. has = false
  522. validatedkey := false
  523. for pk, pv := range currentSubSchema.patternProperties {
  524. if matches, _ := regexp.MatchString(pk, key); matches {
  525. has = true
  526. subContext := newJsonContext(key, context)
  527. validationResult := pv.subValidateWithContext(value, subContext)
  528. result.mergeErrors(validationResult)
  529. if validationResult.Valid() {
  530. validatedkey = true
  531. }
  532. }
  533. }
  534. if !validatedkey {
  535. return has, false
  536. }
  537. result.incrementScore()
  538. return has, true
  539. }
  540. func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {
  541. // Ignore JSON numbers
  542. if isJsonNumber(value) {
  543. return
  544. }
  545. // Ignore non strings
  546. if !isKind(value, reflect.String) {
  547. return
  548. }
  549. if internalLogEnabled {
  550. internalLog("validateString %s", context.String())
  551. internalLog(" %v", value)
  552. }
  553. stringValue := value.(string)
  554. // minLength & maxLength:
  555. if currentSubSchema.minLength != nil {
  556. if utf8.RuneCount([]byte(stringValue)) < int(*currentSubSchema.minLength) {
  557. result.addError(
  558. new(StringLengthGTEError),
  559. context,
  560. value,
  561. ErrorDetails{"min": *currentSubSchema.minLength},
  562. )
  563. }
  564. }
  565. if currentSubSchema.maxLength != nil {
  566. if utf8.RuneCount([]byte(stringValue)) > int(*currentSubSchema.maxLength) {
  567. result.addError(
  568. new(StringLengthLTEError),
  569. context,
  570. value,
  571. ErrorDetails{"max": *currentSubSchema.maxLength},
  572. )
  573. }
  574. }
  575. // pattern:
  576. if currentSubSchema.pattern != nil {
  577. if !currentSubSchema.pattern.MatchString(stringValue) {
  578. result.addError(
  579. new(DoesNotMatchPatternError),
  580. context,
  581. value,
  582. ErrorDetails{"pattern": currentSubSchema.pattern},
  583. )
  584. }
  585. }
  586. // format
  587. if currentSubSchema.format != "" {
  588. if !FormatCheckers.IsFormat(currentSubSchema.format, stringValue) {
  589. result.addError(
  590. new(DoesNotMatchFormatError),
  591. context,
  592. value,
  593. ErrorDetails{"format": currentSubSchema.format},
  594. )
  595. }
  596. }
  597. result.incrementScore()
  598. }
  599. func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {
  600. // Ignore non numbers
  601. if !isJsonNumber(value) {
  602. return
  603. }
  604. if internalLogEnabled {
  605. internalLog("validateNumber %s", context.String())
  606. internalLog(" %v", value)
  607. }
  608. number := value.(json.Number)
  609. float64Value, _ := number.Float64()
  610. // multipleOf:
  611. if currentSubSchema.multipleOf != nil {
  612. if !isFloat64AnInteger(float64Value / *currentSubSchema.multipleOf) {
  613. result.addError(
  614. new(MultipleOfError),
  615. context,
  616. resultErrorFormatJsonNumber(number),
  617. ErrorDetails{"multiple": *currentSubSchema.multipleOf},
  618. )
  619. }
  620. }
  621. //maximum & exclusiveMaximum:
  622. if currentSubSchema.maximum != nil {
  623. if currentSubSchema.exclusiveMaximum {
  624. if float64Value >= *currentSubSchema.maximum {
  625. result.addError(
  626. new(NumberLTEError),
  627. context,
  628. resultErrorFormatJsonNumber(number),
  629. ErrorDetails{
  630. "min": resultErrorFormatNumber(*currentSubSchema.maximum),
  631. },
  632. )
  633. }
  634. } else {
  635. if float64Value > *currentSubSchema.maximum {
  636. result.addError(
  637. new(NumberLTError),
  638. context,
  639. resultErrorFormatJsonNumber(number),
  640. ErrorDetails{
  641. "min": resultErrorFormatNumber(*currentSubSchema.maximum),
  642. },
  643. )
  644. }
  645. }
  646. }
  647. //minimum & exclusiveMinimum:
  648. if currentSubSchema.minimum != nil {
  649. if currentSubSchema.exclusiveMinimum {
  650. if float64Value <= *currentSubSchema.minimum {
  651. result.addError(
  652. new(NumberGTEError),
  653. context,
  654. resultErrorFormatJsonNumber(number),
  655. ErrorDetails{
  656. "max": resultErrorFormatNumber(*currentSubSchema.minimum),
  657. },
  658. )
  659. }
  660. } else {
  661. if float64Value < *currentSubSchema.minimum {
  662. result.addError(
  663. new(NumberGTError),
  664. context,
  665. resultErrorFormatJsonNumber(number),
  666. ErrorDetails{
  667. "max": resultErrorFormatNumber(*currentSubSchema.minimum),
  668. },
  669. )
  670. }
  671. }
  672. }
  673. result.incrementScore()
  674. }