schema.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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 xeipuuv@gmail.com
  17. //
  18. // repository-name gojsonschema
  19. // repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language.
  20. //
  21. // description Defines Schema, the main entry to every subSchema.
  22. // Contains the parsing logic and error checking.
  23. //
  24. // created 26-02-2013
  25. package gojsonschema
  26. import (
  27. "errors"
  28. "math/big"
  29. "reflect"
  30. "regexp"
  31. "text/template"
  32. "github.com/xeipuuv/gojsonreference"
  33. )
  34. var (
  35. // Locale is the default locale to use
  36. // Library users can overwrite with their own implementation
  37. Locale locale = DefaultLocale{}
  38. // ErrorTemplateFuncs allows you to define custom template funcs for use in localization.
  39. ErrorTemplateFuncs template.FuncMap
  40. )
  41. // NewSchema instances a schema using the given JSONLoader
  42. func NewSchema(l JSONLoader) (*Schema, error) {
  43. return NewSchemaLoader().Compile(l)
  44. }
  45. // Schema holds a schema
  46. type Schema struct {
  47. documentReference gojsonreference.JsonReference
  48. rootSchema *subSchema
  49. pool *schemaPool
  50. referencePool *schemaReferencePool
  51. }
  52. func (d *Schema) parse(document interface{}, draft Draft) error {
  53. d.rootSchema = &subSchema{property: STRING_ROOT_SCHEMA_PROPERTY, draft: &draft}
  54. return d.parseSchema(document, d.rootSchema)
  55. }
  56. // SetRootSchemaName sets the root-schema name
  57. func (d *Schema) SetRootSchemaName(name string) {
  58. d.rootSchema.property = name
  59. }
  60. // Parses a subSchema
  61. //
  62. // Pretty long function ( sorry :) )... but pretty straight forward, repetitive and boring
  63. // Not much magic involved here, most of the job is to validate the key names and their values,
  64. // then the values are copied into subSchema struct
  65. //
  66. func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) error {
  67. if currentSchema.draft == nil {
  68. if currentSchema.parent == nil {
  69. return errors.New("Draft not set")
  70. }
  71. currentSchema.draft = currentSchema.parent.draft
  72. }
  73. // As of draft 6 "true" is equivalent to an empty schema "{}" and false equals "{"not":{}}"
  74. if *currentSchema.draft >= Draft6 && isKind(documentNode, reflect.Bool) {
  75. b := documentNode.(bool)
  76. currentSchema.pass = &b
  77. return nil
  78. }
  79. if !isKind(documentNode, reflect.Map) {
  80. return errors.New(formatErrorDescription(
  81. Locale.ParseError(),
  82. ErrorDetails{
  83. "expected": STRING_SCHEMA,
  84. },
  85. ))
  86. }
  87. m := documentNode.(map[string]interface{})
  88. if currentSchema.parent == nil {
  89. currentSchema.ref = &d.documentReference
  90. currentSchema.id = &d.documentReference
  91. }
  92. if currentSchema.id == nil && currentSchema.parent != nil {
  93. currentSchema.id = currentSchema.parent.id
  94. }
  95. // In draft 6 the id keyword was renamed to $id
  96. // Hybrid mode uses the old id by default
  97. var keyID string
  98. switch *currentSchema.draft {
  99. case Draft4:
  100. keyID = KEY_ID
  101. case Hybrid:
  102. keyID = KEY_ID_NEW
  103. if existsMapKey(m, KEY_ID) {
  104. keyID = KEY_ID
  105. }
  106. default:
  107. keyID = KEY_ID_NEW
  108. }
  109. if existsMapKey(m, keyID) && !isKind(m[keyID], reflect.String) {
  110. return errors.New(formatErrorDescription(
  111. Locale.InvalidType(),
  112. ErrorDetails{
  113. "expected": TYPE_STRING,
  114. "given": keyID,
  115. },
  116. ))
  117. }
  118. if k, ok := m[keyID].(string); ok {
  119. jsonReference, err := gojsonreference.NewJsonReference(k)
  120. if err != nil {
  121. return err
  122. }
  123. if currentSchema == d.rootSchema {
  124. currentSchema.id = &jsonReference
  125. } else {
  126. ref, err := currentSchema.parent.id.Inherits(jsonReference)
  127. if err != nil {
  128. return err
  129. }
  130. currentSchema.id = ref
  131. }
  132. }
  133. // definitions
  134. if existsMapKey(m, KEY_DEFINITIONS) {
  135. if isKind(m[KEY_DEFINITIONS], reflect.Map, reflect.Bool) {
  136. for _, dv := range m[KEY_DEFINITIONS].(map[string]interface{}) {
  137. if isKind(dv, reflect.Map, reflect.Bool) {
  138. newSchema := &subSchema{property: KEY_DEFINITIONS, parent: currentSchema}
  139. err := d.parseSchema(dv, newSchema)
  140. if err != nil {
  141. return err
  142. }
  143. } else {
  144. return errors.New(formatErrorDescription(
  145. Locale.InvalidType(),
  146. ErrorDetails{
  147. "expected": STRING_ARRAY_OF_SCHEMAS,
  148. "given": KEY_DEFINITIONS,
  149. },
  150. ))
  151. }
  152. }
  153. } else {
  154. return errors.New(formatErrorDescription(
  155. Locale.InvalidType(),
  156. ErrorDetails{
  157. "expected": STRING_ARRAY_OF_SCHEMAS,
  158. "given": KEY_DEFINITIONS,
  159. },
  160. ))
  161. }
  162. }
  163. // title
  164. if existsMapKey(m, KEY_TITLE) && !isKind(m[KEY_TITLE], reflect.String) {
  165. return errors.New(formatErrorDescription(
  166. Locale.InvalidType(),
  167. ErrorDetails{
  168. "expected": TYPE_STRING,
  169. "given": KEY_TITLE,
  170. },
  171. ))
  172. }
  173. if k, ok := m[KEY_TITLE].(string); ok {
  174. currentSchema.title = &k
  175. }
  176. // description
  177. if existsMapKey(m, KEY_DESCRIPTION) && !isKind(m[KEY_DESCRIPTION], reflect.String) {
  178. return errors.New(formatErrorDescription(
  179. Locale.InvalidType(),
  180. ErrorDetails{
  181. "expected": TYPE_STRING,
  182. "given": KEY_DESCRIPTION,
  183. },
  184. ))
  185. }
  186. if k, ok := m[KEY_DESCRIPTION].(string); ok {
  187. currentSchema.description = &k
  188. }
  189. // $ref
  190. if existsMapKey(m, KEY_REF) && !isKind(m[KEY_REF], reflect.String) {
  191. return errors.New(formatErrorDescription(
  192. Locale.InvalidType(),
  193. ErrorDetails{
  194. "expected": TYPE_STRING,
  195. "given": KEY_REF,
  196. },
  197. ))
  198. }
  199. if k, ok := m[KEY_REF].(string); ok {
  200. jsonReference, err := gojsonreference.NewJsonReference(k)
  201. if err != nil {
  202. return err
  203. }
  204. currentSchema.ref = &jsonReference
  205. if sch, ok := d.referencePool.Get(currentSchema.ref.String()); ok {
  206. currentSchema.refSchema = sch
  207. } else {
  208. err := d.parseReference(documentNode, currentSchema)
  209. if err != nil {
  210. return err
  211. }
  212. return nil
  213. }
  214. }
  215. // type
  216. if existsMapKey(m, KEY_TYPE) {
  217. if isKind(m[KEY_TYPE], reflect.String) {
  218. if k, ok := m[KEY_TYPE].(string); ok {
  219. err := currentSchema.types.Add(k)
  220. if err != nil {
  221. return err
  222. }
  223. }
  224. } else {
  225. if isKind(m[KEY_TYPE], reflect.Slice) {
  226. arrayOfTypes := m[KEY_TYPE].([]interface{})
  227. for _, typeInArray := range arrayOfTypes {
  228. if reflect.ValueOf(typeInArray).Kind() != reflect.String {
  229. return errors.New(formatErrorDescription(
  230. Locale.InvalidType(),
  231. ErrorDetails{
  232. "expected": TYPE_STRING + "/" + STRING_ARRAY_OF_STRINGS,
  233. "given": KEY_TYPE,
  234. },
  235. ))
  236. }
  237. if err := currentSchema.types.Add(typeInArray.(string)); err != nil {
  238. return err
  239. }
  240. }
  241. } else {
  242. return errors.New(formatErrorDescription(
  243. Locale.InvalidType(),
  244. ErrorDetails{
  245. "expected": TYPE_STRING + "/" + STRING_ARRAY_OF_STRINGS,
  246. "given": KEY_TYPE,
  247. },
  248. ))
  249. }
  250. }
  251. }
  252. // properties
  253. if existsMapKey(m, KEY_PROPERTIES) {
  254. err := d.parseProperties(m[KEY_PROPERTIES], currentSchema)
  255. if err != nil {
  256. return err
  257. }
  258. }
  259. // additionalProperties
  260. if existsMapKey(m, KEY_ADDITIONAL_PROPERTIES) {
  261. if isKind(m[KEY_ADDITIONAL_PROPERTIES], reflect.Bool) {
  262. currentSchema.additionalProperties = m[KEY_ADDITIONAL_PROPERTIES].(bool)
  263. } else if isKind(m[KEY_ADDITIONAL_PROPERTIES], reflect.Map) {
  264. newSchema := &subSchema{property: KEY_ADDITIONAL_PROPERTIES, parent: currentSchema, ref: currentSchema.ref}
  265. currentSchema.additionalProperties = newSchema
  266. err := d.parseSchema(m[KEY_ADDITIONAL_PROPERTIES], newSchema)
  267. if err != nil {
  268. return errors.New(err.Error())
  269. }
  270. } else {
  271. return errors.New(formatErrorDescription(
  272. Locale.InvalidType(),
  273. ErrorDetails{
  274. "expected": TYPE_BOOLEAN + "/" + STRING_SCHEMA,
  275. "given": KEY_ADDITIONAL_PROPERTIES,
  276. },
  277. ))
  278. }
  279. }
  280. // patternProperties
  281. if existsMapKey(m, KEY_PATTERN_PROPERTIES) {
  282. if isKind(m[KEY_PATTERN_PROPERTIES], reflect.Map) {
  283. patternPropertiesMap := m[KEY_PATTERN_PROPERTIES].(map[string]interface{})
  284. if len(patternPropertiesMap) > 0 {
  285. currentSchema.patternProperties = make(map[string]*subSchema)
  286. for k, v := range patternPropertiesMap {
  287. _, err := regexp.MatchString(k, "")
  288. if err != nil {
  289. return errors.New(formatErrorDescription(
  290. Locale.RegexPattern(),
  291. ErrorDetails{"pattern": k},
  292. ))
  293. }
  294. newSchema := &subSchema{property: k, parent: currentSchema, ref: currentSchema.ref}
  295. err = d.parseSchema(v, newSchema)
  296. if err != nil {
  297. return errors.New(err.Error())
  298. }
  299. currentSchema.patternProperties[k] = newSchema
  300. }
  301. }
  302. } else {
  303. return errors.New(formatErrorDescription(
  304. Locale.InvalidType(),
  305. ErrorDetails{
  306. "expected": STRING_SCHEMA,
  307. "given": KEY_PATTERN_PROPERTIES,
  308. },
  309. ))
  310. }
  311. }
  312. // propertyNames
  313. if existsMapKey(m, KEY_PROPERTY_NAMES) && *currentSchema.draft >= Draft6 {
  314. if isKind(m[KEY_PROPERTY_NAMES], reflect.Map, reflect.Bool) {
  315. newSchema := &subSchema{property: KEY_PROPERTY_NAMES, parent: currentSchema, ref: currentSchema.ref}
  316. currentSchema.propertyNames = newSchema
  317. err := d.parseSchema(m[KEY_PROPERTY_NAMES], newSchema)
  318. if err != nil {
  319. return err
  320. }
  321. } else {
  322. return errors.New(formatErrorDescription(
  323. Locale.InvalidType(),
  324. ErrorDetails{
  325. "expected": STRING_SCHEMA,
  326. "given": KEY_PATTERN_PROPERTIES,
  327. },
  328. ))
  329. }
  330. }
  331. // dependencies
  332. if existsMapKey(m, KEY_DEPENDENCIES) {
  333. err := d.parseDependencies(m[KEY_DEPENDENCIES], currentSchema)
  334. if err != nil {
  335. return err
  336. }
  337. }
  338. // items
  339. if existsMapKey(m, KEY_ITEMS) {
  340. if isKind(m[KEY_ITEMS], reflect.Slice) {
  341. for _, itemElement := range m[KEY_ITEMS].([]interface{}) {
  342. if isKind(itemElement, reflect.Map, reflect.Bool) {
  343. newSchema := &subSchema{parent: currentSchema, property: KEY_ITEMS}
  344. newSchema.ref = currentSchema.ref
  345. currentSchema.itemsChildren = append(currentSchema.itemsChildren, newSchema)
  346. err := d.parseSchema(itemElement, newSchema)
  347. if err != nil {
  348. return err
  349. }
  350. } else {
  351. return errors.New(formatErrorDescription(
  352. Locale.InvalidType(),
  353. ErrorDetails{
  354. "expected": STRING_SCHEMA + "/" + STRING_ARRAY_OF_SCHEMAS,
  355. "given": KEY_ITEMS,
  356. },
  357. ))
  358. }
  359. currentSchema.itemsChildrenIsSingleSchema = false
  360. }
  361. } else if isKind(m[KEY_ITEMS], reflect.Map, reflect.Bool) {
  362. newSchema := &subSchema{parent: currentSchema, property: KEY_ITEMS}
  363. newSchema.ref = currentSchema.ref
  364. currentSchema.itemsChildren = append(currentSchema.itemsChildren, newSchema)
  365. err := d.parseSchema(m[KEY_ITEMS], newSchema)
  366. if err != nil {
  367. return err
  368. }
  369. currentSchema.itemsChildrenIsSingleSchema = true
  370. } else {
  371. return errors.New(formatErrorDescription(
  372. Locale.InvalidType(),
  373. ErrorDetails{
  374. "expected": STRING_SCHEMA + "/" + STRING_ARRAY_OF_SCHEMAS,
  375. "given": KEY_ITEMS,
  376. },
  377. ))
  378. }
  379. }
  380. // additionalItems
  381. if existsMapKey(m, KEY_ADDITIONAL_ITEMS) {
  382. if isKind(m[KEY_ADDITIONAL_ITEMS], reflect.Bool) {
  383. currentSchema.additionalItems = m[KEY_ADDITIONAL_ITEMS].(bool)
  384. } else if isKind(m[KEY_ADDITIONAL_ITEMS], reflect.Map) {
  385. newSchema := &subSchema{property: KEY_ADDITIONAL_ITEMS, parent: currentSchema, ref: currentSchema.ref}
  386. currentSchema.additionalItems = newSchema
  387. err := d.parseSchema(m[KEY_ADDITIONAL_ITEMS], newSchema)
  388. if err != nil {
  389. return errors.New(err.Error())
  390. }
  391. } else {
  392. return errors.New(formatErrorDescription(
  393. Locale.InvalidType(),
  394. ErrorDetails{
  395. "expected": TYPE_BOOLEAN + "/" + STRING_SCHEMA,
  396. "given": KEY_ADDITIONAL_ITEMS,
  397. },
  398. ))
  399. }
  400. }
  401. // validation : number / integer
  402. if existsMapKey(m, KEY_MULTIPLE_OF) {
  403. multipleOfValue := mustBeNumber(m[KEY_MULTIPLE_OF])
  404. if multipleOfValue == nil {
  405. return errors.New(formatErrorDescription(
  406. Locale.InvalidType(),
  407. ErrorDetails{
  408. "expected": STRING_NUMBER,
  409. "given": KEY_MULTIPLE_OF,
  410. },
  411. ))
  412. }
  413. if multipleOfValue.Cmp(big.NewRat(0, 1)) <= 0 {
  414. return errors.New(formatErrorDescription(
  415. Locale.GreaterThanZero(),
  416. ErrorDetails{"number": KEY_MULTIPLE_OF},
  417. ))
  418. }
  419. currentSchema.multipleOf = multipleOfValue
  420. }
  421. if existsMapKey(m, KEY_MINIMUM) {
  422. minimumValue := mustBeNumber(m[KEY_MINIMUM])
  423. if minimumValue == nil {
  424. return errors.New(formatErrorDescription(
  425. Locale.MustBeOfA(),
  426. ErrorDetails{"x": KEY_MINIMUM, "y": STRING_NUMBER},
  427. ))
  428. }
  429. currentSchema.minimum = minimumValue
  430. }
  431. if existsMapKey(m, KEY_EXCLUSIVE_MINIMUM) {
  432. switch *currentSchema.draft {
  433. case Draft4:
  434. if !isKind(m[KEY_EXCLUSIVE_MINIMUM], reflect.Bool) {
  435. return errors.New(formatErrorDescription(
  436. Locale.InvalidType(),
  437. ErrorDetails{
  438. "expected": TYPE_BOOLEAN,
  439. "given": KEY_EXCLUSIVE_MINIMUM,
  440. },
  441. ))
  442. }
  443. if currentSchema.minimum == nil {
  444. return errors.New(formatErrorDescription(
  445. Locale.CannotBeUsedWithout(),
  446. ErrorDetails{"x": KEY_EXCLUSIVE_MINIMUM, "y": KEY_MINIMUM},
  447. ))
  448. }
  449. if m[KEY_EXCLUSIVE_MINIMUM].(bool) {
  450. currentSchema.exclusiveMinimum = currentSchema.minimum
  451. currentSchema.minimum = nil
  452. }
  453. case Hybrid:
  454. if isKind(m[KEY_EXCLUSIVE_MINIMUM], reflect.Bool) {
  455. if currentSchema.minimum == nil {
  456. return errors.New(formatErrorDescription(
  457. Locale.CannotBeUsedWithout(),
  458. ErrorDetails{"x": KEY_EXCLUSIVE_MINIMUM, "y": KEY_MINIMUM},
  459. ))
  460. }
  461. if m[KEY_EXCLUSIVE_MINIMUM].(bool) {
  462. currentSchema.exclusiveMinimum = currentSchema.minimum
  463. currentSchema.minimum = nil
  464. }
  465. } else if isJSONNumber(m[KEY_EXCLUSIVE_MINIMUM]) {
  466. currentSchema.exclusiveMinimum = mustBeNumber(m[KEY_EXCLUSIVE_MINIMUM])
  467. } else {
  468. return errors.New(formatErrorDescription(
  469. Locale.InvalidType(),
  470. ErrorDetails{
  471. "expected": TYPE_BOOLEAN + "/" + TYPE_NUMBER,
  472. "given": KEY_EXCLUSIVE_MINIMUM,
  473. },
  474. ))
  475. }
  476. default:
  477. if isJSONNumber(m[KEY_EXCLUSIVE_MINIMUM]) {
  478. currentSchema.exclusiveMinimum = mustBeNumber(m[KEY_EXCLUSIVE_MINIMUM])
  479. } else {
  480. return errors.New(formatErrorDescription(
  481. Locale.InvalidType(),
  482. ErrorDetails{
  483. "expected": TYPE_NUMBER,
  484. "given": KEY_EXCLUSIVE_MINIMUM,
  485. },
  486. ))
  487. }
  488. }
  489. }
  490. if existsMapKey(m, KEY_MAXIMUM) {
  491. maximumValue := mustBeNumber(m[KEY_MAXIMUM])
  492. if maximumValue == nil {
  493. return errors.New(formatErrorDescription(
  494. Locale.MustBeOfA(),
  495. ErrorDetails{"x": KEY_MAXIMUM, "y": STRING_NUMBER},
  496. ))
  497. }
  498. currentSchema.maximum = maximumValue
  499. }
  500. if existsMapKey(m, KEY_EXCLUSIVE_MAXIMUM) {
  501. switch *currentSchema.draft {
  502. case Draft4:
  503. if !isKind(m[KEY_EXCLUSIVE_MAXIMUM], reflect.Bool) {
  504. return errors.New(formatErrorDescription(
  505. Locale.InvalidType(),
  506. ErrorDetails{
  507. "expected": TYPE_BOOLEAN,
  508. "given": KEY_EXCLUSIVE_MAXIMUM,
  509. },
  510. ))
  511. }
  512. if currentSchema.maximum == nil {
  513. return errors.New(formatErrorDescription(
  514. Locale.CannotBeUsedWithout(),
  515. ErrorDetails{"x": KEY_EXCLUSIVE_MAXIMUM, "y": KEY_MAXIMUM},
  516. ))
  517. }
  518. if m[KEY_EXCLUSIVE_MAXIMUM].(bool) {
  519. currentSchema.exclusiveMaximum = currentSchema.maximum
  520. currentSchema.maximum = nil
  521. }
  522. case Hybrid:
  523. if isKind(m[KEY_EXCLUSIVE_MAXIMUM], reflect.Bool) {
  524. if currentSchema.maximum == nil {
  525. return errors.New(formatErrorDescription(
  526. Locale.CannotBeUsedWithout(),
  527. ErrorDetails{"x": KEY_EXCLUSIVE_MAXIMUM, "y": KEY_MAXIMUM},
  528. ))
  529. }
  530. if m[KEY_EXCLUSIVE_MAXIMUM].(bool) {
  531. currentSchema.exclusiveMaximum = currentSchema.maximum
  532. currentSchema.maximum = nil
  533. }
  534. } else if isJSONNumber(m[KEY_EXCLUSIVE_MAXIMUM]) {
  535. currentSchema.exclusiveMaximum = mustBeNumber(m[KEY_EXCLUSIVE_MAXIMUM])
  536. } else {
  537. return errors.New(formatErrorDescription(
  538. Locale.InvalidType(),
  539. ErrorDetails{
  540. "expected": TYPE_BOOLEAN + "/" + TYPE_NUMBER,
  541. "given": KEY_EXCLUSIVE_MAXIMUM,
  542. },
  543. ))
  544. }
  545. default:
  546. if isJSONNumber(m[KEY_EXCLUSIVE_MAXIMUM]) {
  547. currentSchema.exclusiveMaximum = mustBeNumber(m[KEY_EXCLUSIVE_MAXIMUM])
  548. } else {
  549. return errors.New(formatErrorDescription(
  550. Locale.InvalidType(),
  551. ErrorDetails{
  552. "expected": TYPE_NUMBER,
  553. "given": KEY_EXCLUSIVE_MAXIMUM,
  554. },
  555. ))
  556. }
  557. }
  558. }
  559. // validation : string
  560. if existsMapKey(m, KEY_MIN_LENGTH) {
  561. minLengthIntegerValue := mustBeInteger(m[KEY_MIN_LENGTH])
  562. if minLengthIntegerValue == nil {
  563. return errors.New(formatErrorDescription(
  564. Locale.MustBeOfAn(),
  565. ErrorDetails{"x": KEY_MIN_LENGTH, "y": TYPE_INTEGER},
  566. ))
  567. }
  568. if *minLengthIntegerValue < 0 {
  569. return errors.New(formatErrorDescription(
  570. Locale.MustBeGTEZero(),
  571. ErrorDetails{"key": KEY_MIN_LENGTH},
  572. ))
  573. }
  574. currentSchema.minLength = minLengthIntegerValue
  575. }
  576. if existsMapKey(m, KEY_MAX_LENGTH) {
  577. maxLengthIntegerValue := mustBeInteger(m[KEY_MAX_LENGTH])
  578. if maxLengthIntegerValue == nil {
  579. return errors.New(formatErrorDescription(
  580. Locale.MustBeOfAn(),
  581. ErrorDetails{"x": KEY_MAX_LENGTH, "y": TYPE_INTEGER},
  582. ))
  583. }
  584. if *maxLengthIntegerValue < 0 {
  585. return errors.New(formatErrorDescription(
  586. Locale.MustBeGTEZero(),
  587. ErrorDetails{"key": KEY_MAX_LENGTH},
  588. ))
  589. }
  590. currentSchema.maxLength = maxLengthIntegerValue
  591. }
  592. if currentSchema.minLength != nil && currentSchema.maxLength != nil {
  593. if *currentSchema.minLength > *currentSchema.maxLength {
  594. return errors.New(formatErrorDescription(
  595. Locale.CannotBeGT(),
  596. ErrorDetails{"x": KEY_MIN_LENGTH, "y": KEY_MAX_LENGTH},
  597. ))
  598. }
  599. }
  600. if existsMapKey(m, KEY_PATTERN) {
  601. if isKind(m[KEY_PATTERN], reflect.String) {
  602. regexpObject, err := regexp.Compile(m[KEY_PATTERN].(string))
  603. if err != nil {
  604. return errors.New(formatErrorDescription(
  605. Locale.MustBeValidRegex(),
  606. ErrorDetails{"key": KEY_PATTERN},
  607. ))
  608. }
  609. currentSchema.pattern = regexpObject
  610. } else {
  611. return errors.New(formatErrorDescription(
  612. Locale.MustBeOfA(),
  613. ErrorDetails{"x": KEY_PATTERN, "y": TYPE_STRING},
  614. ))
  615. }
  616. }
  617. if existsMapKey(m, KEY_FORMAT) {
  618. formatString, ok := m[KEY_FORMAT].(string)
  619. if !ok {
  620. return errors.New(formatErrorDescription(
  621. Locale.MustBeOfType(),
  622. ErrorDetails{"key": KEY_FORMAT, "type": TYPE_STRING},
  623. ))
  624. }
  625. currentSchema.format = formatString
  626. }
  627. // validation : object
  628. if existsMapKey(m, KEY_MIN_PROPERTIES) {
  629. minPropertiesIntegerValue := mustBeInteger(m[KEY_MIN_PROPERTIES])
  630. if minPropertiesIntegerValue == nil {
  631. return errors.New(formatErrorDescription(
  632. Locale.MustBeOfAn(),
  633. ErrorDetails{"x": KEY_MIN_PROPERTIES, "y": TYPE_INTEGER},
  634. ))
  635. }
  636. if *minPropertiesIntegerValue < 0 {
  637. return errors.New(formatErrorDescription(
  638. Locale.MustBeGTEZero(),
  639. ErrorDetails{"key": KEY_MIN_PROPERTIES},
  640. ))
  641. }
  642. currentSchema.minProperties = minPropertiesIntegerValue
  643. }
  644. if existsMapKey(m, KEY_MAX_PROPERTIES) {
  645. maxPropertiesIntegerValue := mustBeInteger(m[KEY_MAX_PROPERTIES])
  646. if maxPropertiesIntegerValue == nil {
  647. return errors.New(formatErrorDescription(
  648. Locale.MustBeOfAn(),
  649. ErrorDetails{"x": KEY_MAX_PROPERTIES, "y": TYPE_INTEGER},
  650. ))
  651. }
  652. if *maxPropertiesIntegerValue < 0 {
  653. return errors.New(formatErrorDescription(
  654. Locale.MustBeGTEZero(),
  655. ErrorDetails{"key": KEY_MAX_PROPERTIES},
  656. ))
  657. }
  658. currentSchema.maxProperties = maxPropertiesIntegerValue
  659. }
  660. if currentSchema.minProperties != nil && currentSchema.maxProperties != nil {
  661. if *currentSchema.minProperties > *currentSchema.maxProperties {
  662. return errors.New(formatErrorDescription(
  663. Locale.KeyCannotBeGreaterThan(),
  664. ErrorDetails{"key": KEY_MIN_PROPERTIES, "y": KEY_MAX_PROPERTIES},
  665. ))
  666. }
  667. }
  668. if existsMapKey(m, KEY_REQUIRED) {
  669. if isKind(m[KEY_REQUIRED], reflect.Slice) {
  670. requiredValues := m[KEY_REQUIRED].([]interface{})
  671. for _, requiredValue := range requiredValues {
  672. if isKind(requiredValue, reflect.String) {
  673. if isStringInSlice(currentSchema.required, requiredValue.(string)) {
  674. return errors.New(formatErrorDescription(
  675. Locale.KeyItemsMustBeUnique(),
  676. ErrorDetails{"key": KEY_REQUIRED},
  677. ))
  678. }
  679. currentSchema.required = append(currentSchema.required, requiredValue.(string))
  680. } else {
  681. return errors.New(formatErrorDescription(
  682. Locale.KeyItemsMustBeOfType(),
  683. ErrorDetails{"key": KEY_REQUIRED, "type": TYPE_STRING},
  684. ))
  685. }
  686. }
  687. } else {
  688. return errors.New(formatErrorDescription(
  689. Locale.MustBeOfAn(),
  690. ErrorDetails{"x": KEY_REQUIRED, "y": TYPE_ARRAY},
  691. ))
  692. }
  693. }
  694. // validation : array
  695. if existsMapKey(m, KEY_MIN_ITEMS) {
  696. minItemsIntegerValue := mustBeInteger(m[KEY_MIN_ITEMS])
  697. if minItemsIntegerValue == nil {
  698. return errors.New(formatErrorDescription(
  699. Locale.MustBeOfAn(),
  700. ErrorDetails{"x": KEY_MIN_ITEMS, "y": TYPE_INTEGER},
  701. ))
  702. }
  703. if *minItemsIntegerValue < 0 {
  704. return errors.New(formatErrorDescription(
  705. Locale.MustBeGTEZero(),
  706. ErrorDetails{"key": KEY_MIN_ITEMS},
  707. ))
  708. }
  709. currentSchema.minItems = minItemsIntegerValue
  710. }
  711. if existsMapKey(m, KEY_MAX_ITEMS) {
  712. maxItemsIntegerValue := mustBeInteger(m[KEY_MAX_ITEMS])
  713. if maxItemsIntegerValue == nil {
  714. return errors.New(formatErrorDescription(
  715. Locale.MustBeOfAn(),
  716. ErrorDetails{"x": KEY_MAX_ITEMS, "y": TYPE_INTEGER},
  717. ))
  718. }
  719. if *maxItemsIntegerValue < 0 {
  720. return errors.New(formatErrorDescription(
  721. Locale.MustBeGTEZero(),
  722. ErrorDetails{"key": KEY_MAX_ITEMS},
  723. ))
  724. }
  725. currentSchema.maxItems = maxItemsIntegerValue
  726. }
  727. if existsMapKey(m, KEY_UNIQUE_ITEMS) {
  728. if isKind(m[KEY_UNIQUE_ITEMS], reflect.Bool) {
  729. currentSchema.uniqueItems = m[KEY_UNIQUE_ITEMS].(bool)
  730. } else {
  731. return errors.New(formatErrorDescription(
  732. Locale.MustBeOfA(),
  733. ErrorDetails{"x": KEY_UNIQUE_ITEMS, "y": TYPE_BOOLEAN},
  734. ))
  735. }
  736. }
  737. if existsMapKey(m, KEY_CONTAINS) && *currentSchema.draft >= Draft6 {
  738. newSchema := &subSchema{property: KEY_CONTAINS, parent: currentSchema, ref: currentSchema.ref}
  739. currentSchema.contains = newSchema
  740. err := d.parseSchema(m[KEY_CONTAINS], newSchema)
  741. if err != nil {
  742. return err
  743. }
  744. }
  745. // validation : all
  746. if existsMapKey(m, KEY_CONST) && *currentSchema.draft >= Draft6 {
  747. is, err := marshalWithoutNumber(m[KEY_CONST])
  748. if err != nil {
  749. return err
  750. }
  751. currentSchema._const = is
  752. }
  753. if existsMapKey(m, KEY_ENUM) {
  754. if isKind(m[KEY_ENUM], reflect.Slice) {
  755. for _, v := range m[KEY_ENUM].([]interface{}) {
  756. is, err := marshalWithoutNumber(v)
  757. if err != nil {
  758. return err
  759. }
  760. if isStringInSlice(currentSchema.enum, *is) {
  761. return errors.New(formatErrorDescription(
  762. Locale.KeyItemsMustBeUnique(),
  763. ErrorDetails{"key": KEY_ENUM},
  764. ))
  765. }
  766. currentSchema.enum = append(currentSchema.enum, *is)
  767. }
  768. } else {
  769. return errors.New(formatErrorDescription(
  770. Locale.MustBeOfAn(),
  771. ErrorDetails{"x": KEY_ENUM, "y": TYPE_ARRAY},
  772. ))
  773. }
  774. }
  775. // validation : subSchema
  776. if existsMapKey(m, KEY_ONE_OF) {
  777. if isKind(m[KEY_ONE_OF], reflect.Slice) {
  778. for _, v := range m[KEY_ONE_OF].([]interface{}) {
  779. newSchema := &subSchema{property: KEY_ONE_OF, parent: currentSchema, ref: currentSchema.ref}
  780. currentSchema.oneOf = append(currentSchema.oneOf, newSchema)
  781. err := d.parseSchema(v, newSchema)
  782. if err != nil {
  783. return err
  784. }
  785. }
  786. } else {
  787. return errors.New(formatErrorDescription(
  788. Locale.MustBeOfAn(),
  789. ErrorDetails{"x": KEY_ONE_OF, "y": TYPE_ARRAY},
  790. ))
  791. }
  792. }
  793. if existsMapKey(m, KEY_ANY_OF) {
  794. if isKind(m[KEY_ANY_OF], reflect.Slice) {
  795. for _, v := range m[KEY_ANY_OF].([]interface{}) {
  796. newSchema := &subSchema{property: KEY_ANY_OF, parent: currentSchema, ref: currentSchema.ref}
  797. currentSchema.anyOf = append(currentSchema.anyOf, newSchema)
  798. err := d.parseSchema(v, newSchema)
  799. if err != nil {
  800. return err
  801. }
  802. }
  803. } else {
  804. return errors.New(formatErrorDescription(
  805. Locale.MustBeOfAn(),
  806. ErrorDetails{"x": KEY_ANY_OF, "y": TYPE_ARRAY},
  807. ))
  808. }
  809. }
  810. if existsMapKey(m, KEY_ALL_OF) {
  811. if isKind(m[KEY_ALL_OF], reflect.Slice) {
  812. for _, v := range m[KEY_ALL_OF].([]interface{}) {
  813. newSchema := &subSchema{property: KEY_ALL_OF, parent: currentSchema, ref: currentSchema.ref}
  814. currentSchema.allOf = append(currentSchema.allOf, newSchema)
  815. err := d.parseSchema(v, newSchema)
  816. if err != nil {
  817. return err
  818. }
  819. }
  820. } else {
  821. return errors.New(formatErrorDescription(
  822. Locale.MustBeOfAn(),
  823. ErrorDetails{"x": KEY_ANY_OF, "y": TYPE_ARRAY},
  824. ))
  825. }
  826. }
  827. if existsMapKey(m, KEY_NOT) {
  828. if isKind(m[KEY_NOT], reflect.Map, reflect.Bool) {
  829. newSchema := &subSchema{property: KEY_NOT, parent: currentSchema, ref: currentSchema.ref}
  830. currentSchema.not = newSchema
  831. err := d.parseSchema(m[KEY_NOT], newSchema)
  832. if err != nil {
  833. return err
  834. }
  835. } else {
  836. return errors.New(formatErrorDescription(
  837. Locale.MustBeOfAn(),
  838. ErrorDetails{"x": KEY_NOT, "y": TYPE_OBJECT},
  839. ))
  840. }
  841. }
  842. if *currentSchema.draft >= Draft7 {
  843. if existsMapKey(m, KEY_IF) {
  844. if isKind(m[KEY_IF], reflect.Map, reflect.Bool) {
  845. newSchema := &subSchema{property: KEY_IF, parent: currentSchema, ref: currentSchema.ref}
  846. currentSchema._if = newSchema
  847. err := d.parseSchema(m[KEY_IF], newSchema)
  848. if err != nil {
  849. return err
  850. }
  851. } else {
  852. return errors.New(formatErrorDescription(
  853. Locale.MustBeOfAn(),
  854. ErrorDetails{"x": KEY_IF, "y": TYPE_OBJECT},
  855. ))
  856. }
  857. }
  858. if existsMapKey(m, KEY_THEN) {
  859. if isKind(m[KEY_THEN], reflect.Map, reflect.Bool) {
  860. newSchema := &subSchema{property: KEY_THEN, parent: currentSchema, ref: currentSchema.ref}
  861. currentSchema._then = newSchema
  862. err := d.parseSchema(m[KEY_THEN], newSchema)
  863. if err != nil {
  864. return err
  865. }
  866. } else {
  867. return errors.New(formatErrorDescription(
  868. Locale.MustBeOfAn(),
  869. ErrorDetails{"x": KEY_THEN, "y": TYPE_OBJECT},
  870. ))
  871. }
  872. }
  873. if existsMapKey(m, KEY_ELSE) {
  874. if isKind(m[KEY_ELSE], reflect.Map, reflect.Bool) {
  875. newSchema := &subSchema{property: KEY_ELSE, parent: currentSchema, ref: currentSchema.ref}
  876. currentSchema._else = newSchema
  877. err := d.parseSchema(m[KEY_ELSE], newSchema)
  878. if err != nil {
  879. return err
  880. }
  881. } else {
  882. return errors.New(formatErrorDescription(
  883. Locale.MustBeOfAn(),
  884. ErrorDetails{"x": KEY_ELSE, "y": TYPE_OBJECT},
  885. ))
  886. }
  887. }
  888. }
  889. return nil
  890. }
  891. func (d *Schema) parseReference(documentNode interface{}, currentSchema *subSchema) error {
  892. var (
  893. refdDocumentNode interface{}
  894. dsp *schemaPoolDocument
  895. err error
  896. )
  897. newSchema := &subSchema{property: KEY_REF, parent: currentSchema, ref: currentSchema.ref}
  898. d.referencePool.Add(currentSchema.ref.String(), newSchema)
  899. dsp, err = d.pool.GetDocument(*currentSchema.ref)
  900. if err != nil {
  901. return err
  902. }
  903. newSchema.id = currentSchema.ref
  904. refdDocumentNode = dsp.Document
  905. newSchema.draft = dsp.Draft
  906. if err != nil {
  907. return err
  908. }
  909. if !isKind(refdDocumentNode, reflect.Map, reflect.Bool) {
  910. return errors.New(formatErrorDescription(
  911. Locale.MustBeOfType(),
  912. ErrorDetails{"key": STRING_SCHEMA, "type": TYPE_OBJECT},
  913. ))
  914. }
  915. err = d.parseSchema(refdDocumentNode, newSchema)
  916. if err != nil {
  917. return err
  918. }
  919. currentSchema.refSchema = newSchema
  920. return nil
  921. }
  922. func (d *Schema) parseProperties(documentNode interface{}, currentSchema *subSchema) error {
  923. if !isKind(documentNode, reflect.Map) {
  924. return errors.New(formatErrorDescription(
  925. Locale.MustBeOfType(),
  926. ErrorDetails{"key": STRING_PROPERTIES, "type": TYPE_OBJECT},
  927. ))
  928. }
  929. m := documentNode.(map[string]interface{})
  930. for k := range m {
  931. schemaProperty := k
  932. newSchema := &subSchema{property: schemaProperty, parent: currentSchema, ref: currentSchema.ref}
  933. currentSchema.propertiesChildren = append(currentSchema.propertiesChildren, newSchema)
  934. err := d.parseSchema(m[k], newSchema)
  935. if err != nil {
  936. return err
  937. }
  938. }
  939. return nil
  940. }
  941. func (d *Schema) parseDependencies(documentNode interface{}, currentSchema *subSchema) error {
  942. if !isKind(documentNode, reflect.Map) {
  943. return errors.New(formatErrorDescription(
  944. Locale.MustBeOfType(),
  945. ErrorDetails{"key": KEY_DEPENDENCIES, "type": TYPE_OBJECT},
  946. ))
  947. }
  948. m := documentNode.(map[string]interface{})
  949. currentSchema.dependencies = make(map[string]interface{})
  950. for k := range m {
  951. switch reflect.ValueOf(m[k]).Kind() {
  952. case reflect.Slice:
  953. values := m[k].([]interface{})
  954. var valuesToRegister []string
  955. for _, value := range values {
  956. if !isKind(value, reflect.String) {
  957. return errors.New(formatErrorDescription(
  958. Locale.MustBeOfType(),
  959. ErrorDetails{
  960. "key": STRING_DEPENDENCY,
  961. "type": STRING_SCHEMA_OR_ARRAY_OF_STRINGS,
  962. },
  963. ))
  964. }
  965. valuesToRegister = append(valuesToRegister, value.(string))
  966. currentSchema.dependencies[k] = valuesToRegister
  967. }
  968. case reflect.Map, reflect.Bool:
  969. depSchema := &subSchema{property: k, parent: currentSchema, ref: currentSchema.ref}
  970. err := d.parseSchema(m[k], depSchema)
  971. if err != nil {
  972. return err
  973. }
  974. currentSchema.dependencies[k] = depSchema
  975. default:
  976. return errors.New(formatErrorDescription(
  977. Locale.MustBeOfType(),
  978. ErrorDetails{
  979. "key": STRING_DEPENDENCY,
  980. "type": STRING_SCHEMA_OR_ARRAY_OF_STRINGS,
  981. },
  982. ))
  983. }
  984. }
  985. return nil
  986. }