validation.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>JSON Editor Validation Example</title>
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  7. <script src='../../dist/jsoneditor.js'></script>
  8. <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjs/2.7.0/math.min.js'></script>
  9. </head>
  10. <body>
  11. <div id='output'></div>
  12. <script>
  13. var tests = {
  14. required3: {
  15. schema: {
  16. type: "object",
  17. properties: {
  18. required: {
  19. type: "string",
  20. required: true
  21. },
  22. notrequired: {
  23. type: "string",
  24. required: false
  25. },
  26. defaultrequired: {
  27. type: "string"
  28. }
  29. }
  30. },
  31. valid: [
  32. {
  33. required: ""
  34. },
  35. {
  36. required: "",
  37. notrequired: "",
  38. defaultrequired: ""
  39. }
  40. ],
  41. invalid: [
  42. {
  43. notrequired: ""
  44. }
  45. ]
  46. },
  47. enum: {
  48. schema: {
  49. type: "string",
  50. enum: ["value1","value2"]
  51. },
  52. valid: ["value1","value2"],
  53. invalid: ["value3"]
  54. },
  55. extends: {
  56. schema: {
  57. type: "string",
  58. extends: [
  59. {
  60. minLength: "5"
  61. },
  62. {
  63. maxLength: "10"
  64. }
  65. ]
  66. },
  67. valid: ["abcdef","abcdefghij","abcdefgh"],
  68. invalid: ["abcd","abcdefghijk"]
  69. },
  70. allOf: {
  71. schema: {
  72. type: "string",
  73. allOf: [
  74. {
  75. minLength: "5"
  76. },
  77. {
  78. maxLength: "10"
  79. }
  80. ]
  81. },
  82. valid: ["abcdef","abcdefghij","abcdefgh"],
  83. invalid: ["abcd","abcdefghijk"]
  84. },
  85. anyOf: {
  86. schema: {
  87. type: "string",
  88. anyOf: [
  89. {
  90. minLength: "5"
  91. },
  92. {
  93. pattern: '^[0-9]+$'
  94. }
  95. ]
  96. },
  97. valid: ["abcdef","123","12345"],
  98. invalid: ["a"]
  99. },
  100. oneOf: {
  101. schema: {
  102. type: "string",
  103. oneOf: [
  104. {
  105. minLength: "5"
  106. },
  107. {
  108. pattern: '^[0-9]+$'
  109. }
  110. ]
  111. },
  112. valid: ["abcdef","123"],
  113. invalid: ["a","12345"]
  114. },
  115. not: {
  116. schema: {
  117. type: "string",
  118. not: {
  119. pattern: '^[0-9]+$'
  120. }
  121. },
  122. valid: ["abc"],
  123. invalid: ["123"]
  124. },
  125. type: {
  126. schema: {
  127. type: "object",
  128. properties: {
  129. string: {
  130. type: "string"
  131. },
  132. integer: {
  133. type: "integer"
  134. },
  135. number: {
  136. type: "number"
  137. },
  138. array: {
  139. type: "array"
  140. },
  141. object: {
  142. type: "object"
  143. },
  144. boolean: {
  145. type: "boolean"
  146. },
  147. null: {
  148. type: "null"
  149. },
  150. any: {
  151. type: "any"
  152. },
  153. undefined: {
  154. },
  155. stringnumber: {
  156. type: ["string","number"]
  157. },
  158. schema: {
  159. type: [{
  160. type: "string",
  161. minLength: "5"
  162. }]
  163. }
  164. }
  165. },
  166. valid: [{
  167. string: "hello",
  168. number: 1.50,
  169. integer: 1,
  170. boolean: true,
  171. array: [],
  172. object: {},
  173. null: null,
  174. any: "test",
  175. undefined: "test",
  176. stringnumber: "a",
  177. schema: "abcdef"
  178. },{
  179. stringnumber: 5
  180. }],
  181. invalid: [{
  182. string: 1
  183. },{
  184. number: "test"
  185. },{
  186. integer: 1.5
  187. },{
  188. integer: "test"
  189. },{
  190. boolean: "true"
  191. },{
  192. array: {}
  193. },{
  194. object: []
  195. },{
  196. null: {}
  197. },{
  198. stringnumber: true
  199. },{
  200. schema: "abc"
  201. },{
  202. schema: 5
  203. }]
  204. },
  205. multipleOf: {
  206. schema: {
  207. multipleOf: 5
  208. },
  209. valid: [5,0,10],
  210. invalid: [5.5,8,1]
  211. },
  212. divisibleBy: {
  213. schema: {
  214. divisibleBy: 5
  215. },
  216. valid: [5,0,10],
  217. invalid: [5.5,8,1]
  218. },
  219. multipleOfDecimal: {
  220. schema: {
  221. multipleOf: .01
  222. },
  223. valid: [1,1.14,3.57,56],
  224. invalid: [1.012]
  225. },
  226. minmax: {
  227. schema: {
  228. type: "object",
  229. properties: {
  230. min: {
  231. minimum: 5
  232. },
  233. exmin: {
  234. minimum: 5,
  235. exclusiveMinimum: true
  236. },
  237. max: {
  238. maximum: 5
  239. },
  240. exmax: {
  241. maximum: 5,
  242. exclusiveMaximum: true
  243. }
  244. }
  245. },
  246. valid: [{
  247. min: 5,
  248. exmin: 6,
  249. max: 5,
  250. exmax: 4
  251. }],
  252. invalid: [{min: 4},{exmin:5},{max:6},{exmax:5}]
  253. },
  254. minmaxLength: {
  255. schema: {
  256. type: "object",
  257. properties: {
  258. min: {
  259. minLength: 5
  260. },
  261. max: {
  262. maxLength: 5
  263. }
  264. }
  265. },
  266. valid: [{
  267. min: "abcde",
  268. max: "abcde"
  269. },{
  270. min: "abcdef",
  271. max: "abcd"
  272. }],
  273. invalid: [{min: "abcd"},{max:"abcdef"}]
  274. },
  275. pattern: {
  276. schema: {
  277. pattern: "[0-9]"
  278. },
  279. valid: ["abc123"],
  280. invalid: ["abc"]
  281. },
  282. items_schema: {
  283. schema: {
  284. items: {
  285. type: "string"
  286. }
  287. },
  288. valid: [
  289. ["test",""],
  290. []
  291. ],
  292. invalid: [
  293. ["test",123]
  294. ]
  295. },
  296. items_array: {
  297. schema: {
  298. items: [{
  299. type: "string"
  300. },{
  301. type: "number"
  302. }],
  303. additionalItems: false
  304. },
  305. valid: [
  306. ["test",123]
  307. ],
  308. invalid: [
  309. ["test",123,123],
  310. [123,"test"]
  311. ]
  312. },
  313. additionalItems: {
  314. schema: {
  315. items: [{
  316. type: "string"
  317. }],
  318. additionalItems: {
  319. type: "number"
  320. }
  321. },
  322. valid: [
  323. ["test",1],
  324. ["test"]
  325. ],
  326. invalid: [
  327. ["test",1,"test"],
  328. ["test","test2"]
  329. ]
  330. },
  331. minmaxItems: {
  332. schema: {
  333. type: "object",
  334. properties: {
  335. min: {
  336. minItems: 3
  337. },
  338. max: {
  339. maxItems: 3
  340. }
  341. }
  342. },
  343. valid: [
  344. {
  345. min: [1,2,3],
  346. max: [1,2,3]
  347. },
  348. {
  349. min: [1,2,3,4],
  350. max: [1,2]
  351. }
  352. ],
  353. invalid: [
  354. {
  355. min: [1,2]
  356. },
  357. {
  358. max: [1,2,3,4]
  359. }
  360. ]
  361. },
  362. uniqueItems: {
  363. schema: {
  364. uniqueItems: true
  365. },
  366. valid: [[1,2,3,{a:1},"1","{a:1}"]],
  367. invalid: [
  368. [1,1,2],
  369. [{a: 1},{a: 1}]
  370. ]
  371. },
  372. minmaxProperties: {
  373. schema: {
  374. type: "object",
  375. properties: {
  376. min: {
  377. minProperties: 2
  378. },
  379. max: {
  380. maxProperties: 2
  381. }
  382. }
  383. },
  384. valid: [
  385. {
  386. min: {
  387. a: 1,
  388. b: 2
  389. },
  390. max: {
  391. a: 1,
  392. b: 2
  393. }
  394. },
  395. {
  396. min: {
  397. a: 1,
  398. b: 2,
  399. c: 3
  400. },
  401. max: {
  402. a: 1
  403. }
  404. }
  405. ],
  406. invalid: [
  407. {
  408. min: {
  409. a: 1
  410. }
  411. },
  412. {
  413. max: {
  414. a: 1,
  415. b: 2,
  416. c: 3
  417. }
  418. }
  419. ]
  420. },
  421. required4: {
  422. schema: {
  423. type: "object",
  424. properties: {
  425. required: {}
  426. },
  427. required: ["required"],
  428. additionalProperties: true
  429. },
  430. valid: [{required: true, test: 1}],
  431. invalid: [
  432. {}
  433. ]
  434. },
  435. required4_merge: {
  436. schema: {
  437. type: "object",
  438. properties: {
  439. orig: {},
  440. other: {}
  441. },
  442. required: ["orig"],
  443. oneOf: [
  444. {
  445. required: ["other"]
  446. }
  447. ]
  448. },
  449. valid: [
  450. {
  451. orig: true,
  452. other: true
  453. }
  454. ],
  455. invalid: [
  456. {
  457. orig: true
  458. },
  459. {
  460. other: true
  461. }
  462. ]
  463. },
  464. properties: {
  465. schema: {
  466. type: "object",
  467. properties: {
  468. required: {
  469. type: "string"
  470. }
  471. },
  472. additionalProperties: {
  473. type: "number"
  474. }
  475. },
  476. valid: [{required: "test"},{other: 1}],
  477. invalid: [
  478. {other: "test"},
  479. {required: 1}
  480. ]
  481. },
  482. patternProperties: {
  483. schema: {
  484. type: "object",
  485. patternProperties: {
  486. "^test": {
  487. type: "string"
  488. }
  489. },
  490. additionalProperties: false
  491. },
  492. valid: [
  493. {testing: "test"},
  494. {}
  495. ],
  496. invalid: [
  497. {testing: 1},
  498. {other: "five"}
  499. ]
  500. },
  501. dependencies: {
  502. schema: {
  503. type: "object",
  504. dependencies: {
  505. property: ['property1','property2'],
  506. schema: {
  507. properties: {
  508. schema2: {
  509. type: "string"
  510. }
  511. }
  512. }
  513. }
  514. },
  515. valid: [
  516. {
  517. property: "",
  518. property1: "",
  519. property2: "",
  520. schema2: 1
  521. },
  522. {
  523. schema: ""
  524. },
  525. {
  526. schema: "",
  527. schema2: ""
  528. }
  529. ],
  530. invalid: [
  531. {
  532. property: "",
  533. property1: ""
  534. },
  535. {
  536. schema: "",
  537. schema2: 2
  538. }
  539. ]
  540. },
  541. definitions: {
  542. schema: {
  543. type: "object",
  544. properties: {
  545. storage: {
  546. $ref: "#/definitions/diskDevice"
  547. },
  548. external: {
  549. $ref: "#/definitions/external"
  550. }
  551. },
  552. definitions: {
  553. diskDevice: {
  554. pattern: "^/dev/[^/]+(/[^/]+)*$"
  555. },
  556. external: {
  557. "$ref": "string.json"
  558. }
  559. }
  560. },
  561. valid: [
  562. {
  563. storage: "/dev/test",
  564. external: "test"
  565. }
  566. ],
  567. invalid: [
  568. {
  569. storage: "test"
  570. },
  571. {
  572. external: 1
  573. }
  574. ]
  575. },
  576. definitions_nested: {
  577. schema: {
  578. type: "object",
  579. properties: {
  580. id: {
  581. $ref: "#/definitions/app/definitions/id"
  582. },
  583. definitions: {
  584. type: "number"
  585. },
  586. test: {
  587. $ref: "#/definitions/test"
  588. }
  589. },
  590. definitions: {
  591. app: {
  592. definitions: {
  593. id: {
  594. type: "string"
  595. }
  596. }
  597. },
  598. test: {
  599. properties: {
  600. definitions: {
  601. type: "number"
  602. }
  603. }
  604. }
  605. }
  606. },
  607. valid: [
  608. {
  609. id: "test",
  610. definitions: 3,
  611. test: {definitions: 3}
  612. }
  613. ],
  614. invalid: [
  615. {
  616. id: 1
  617. },
  618. {
  619. definitions: "string"
  620. },
  621. {
  622. test: {definitions: "string"}
  623. }
  624. ]
  625. },
  626. $ref: {
  627. schema: {
  628. type: "object",
  629. properties: {
  630. "/": { $ref: 'string.json' }
  631. }
  632. },
  633. valid: [
  634. {
  635. "/": "test"
  636. }
  637. ],
  638. invalid: [
  639. {
  640. "/": 1
  641. }
  642. ]
  643. },
  644. $ref_recursive: {
  645. schema: {
  646. type: "object",
  647. properties: {
  648. "recursive": { $ref: 'recursive.json' },
  649. "string": {
  650. $ref: "string.json"
  651. }
  652. }
  653. },
  654. valid: [
  655. {
  656. "recursive": {
  657. "string": "test"
  658. },
  659. "string": "test"
  660. }
  661. ],
  662. invalid: [
  663. {
  664. "recursive": "test"
  665. },
  666. {
  667. "recursive": {
  668. "string": 1
  669. }
  670. },
  671. {
  672. "string": 1
  673. }
  674. ]
  675. },
  676. disallow: {
  677. schema: {
  678. type: "object",
  679. properties: {
  680. union: {
  681. disallow: [
  682. "string",
  683. {
  684. type: "number",
  685. maximum: 5
  686. }
  687. ]
  688. },
  689. simple: {
  690. disallow: "number"
  691. }
  692. }
  693. },
  694. valid: [
  695. {
  696. union: 7,
  697. simple: "test"
  698. },
  699. {
  700. union: [],
  701. simple: {}
  702. }
  703. ],
  704. invalid: [
  705. {
  706. union: 4
  707. },
  708. {
  709. union: "test"
  710. },
  711. {
  712. simple: 3
  713. }
  714. ]
  715. },
  716. custom_validation: {
  717. schema: {
  718. type: "string",
  719. format: "date"
  720. },
  721. valid: ["1999-01-01"],
  722. invalid: ["1999","abc","abc 1999-01-01","1999-01-01 abc"]
  723. }
  724. };
  725. // Custom validators must return an array of errors or an empty array if valid
  726. $.jsoneditor.custom_validators.push(function(schema, value, path) {
  727. var errors = [];
  728. if(schema.format==="date") {
  729. if(!/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(value)) {
  730. // Errors must be an object with `path`, `property`, and `mesage`
  731. errors.push({
  732. path: path,
  733. property: 'format',
  734. message: 'Dates must be in the format "YYYY-MM-DD"'
  735. });
  736. }
  737. }
  738. return errors;
  739. });
  740. var num = 0;
  741. var animel = $("<div></div>");
  742. $.each(tests,function(i,test) {
  743. animel.queue(function(next) {
  744. console.log(i);
  745. try {
  746. var editor = new JSONEditor(document.createElement('div'),{
  747. schema: test.schema,
  748. ajax: true
  749. });
  750. }
  751. catch(e) {
  752. console.log(test.schema);
  753. throw e;
  754. }
  755. editor.on('ready',function() {
  756. $.each(test.valid,function(j,value) {
  757. try {
  758. num++;
  759. var result = editor.validate(value);
  760. if(result.length) {
  761. console.error(num,'valid',j,JSON.stringify(result,null,2));
  762. $("#output").append("<div><strong>"+i+" test "+j+"</strong>:: Expected: [], Actual: "+JSON.stringify(result)+"</div>");
  763. }
  764. else {
  765. console.log(num,'valid',j);
  766. }
  767. }
  768. catch(e) {
  769. console.log(test.schema,value);
  770. throw e;
  771. }
  772. });
  773. $.each(test.invalid,function(j,value) {
  774. try {
  775. num++;
  776. var result = editor.validate(value);
  777. if(!result.length) {
  778. console.error(num,'invalid',j,JSON.stringify(result,null,2));
  779. $("#output").append("<div><strong>"+i+" test "+j+"</strong>:: Expected: errors, Actual: []</div>");
  780. }
  781. else {
  782. var errors = [];
  783. $.each(result,function(k,error) {
  784. errors.push(error.path+": "+error.message);
  785. });
  786. if(errors.length === 1) errors = errors[0];
  787. console.log(num,'invalid',j,JSON.stringify(errors,null,2));
  788. }
  789. }
  790. catch(e) {
  791. console.log(test.schema,value);
  792. throw e;
  793. }
  794. });
  795. next();
  796. });
  797. });
  798. });
  799. </script>
  800. </body>
  801. </html>