123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>JSON Editor Validation Example</title>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
- <script src='../../dist/jsoneditor.js'></script>
- <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjs/2.7.0/math.min.js'></script>
- </head>
- <body>
- <div id='output'></div>
- <script>
- var tests = {
- required3: {
- schema: {
- type: "object",
- properties: {
- required: {
- type: "string",
- required: true
- },
- notrequired: {
- type: "string",
- required: false
- },
- defaultrequired: {
- type: "string"
- }
- }
- },
- valid: [
- {
- required: ""
- },
- {
- required: "",
- notrequired: "",
- defaultrequired: ""
- }
- ],
- invalid: [
- {
- notrequired: ""
- }
- ]
- },
- enum: {
- schema: {
- type: "string",
- enum: ["value1","value2"]
- },
- valid: ["value1","value2"],
- invalid: ["value3"]
- },
- extends: {
- schema: {
- type: "string",
- extends: [
- {
- minLength: "5"
- },
- {
- maxLength: "10"
- }
- ]
- },
- valid: ["abcdef","abcdefghij","abcdefgh"],
- invalid: ["abcd","abcdefghijk"]
- },
- allOf: {
- schema: {
- type: "string",
- allOf: [
- {
- minLength: "5"
- },
- {
- maxLength: "10"
- }
- ]
- },
- valid: ["abcdef","abcdefghij","abcdefgh"],
- invalid: ["abcd","abcdefghijk"]
- },
- anyOf: {
- schema: {
- type: "string",
- anyOf: [
- {
- minLength: "5"
- },
- {
- pattern: '^[0-9]+$'
- }
- ]
- },
- valid: ["abcdef","123","12345"],
- invalid: ["a"]
- },
- oneOf: {
- schema: {
- type: "string",
- oneOf: [
- {
- minLength: "5"
- },
- {
- pattern: '^[0-9]+$'
- }
- ]
- },
- valid: ["abcdef","123"],
- invalid: ["a","12345"]
- },
- not: {
- schema: {
- type: "string",
- not: {
- pattern: '^[0-9]+$'
- }
- },
- valid: ["abc"],
- invalid: ["123"]
- },
- type: {
- schema: {
- type: "object",
- properties: {
- string: {
- type: "string"
- },
- integer: {
- type: "integer"
- },
- number: {
- type: "number"
- },
- array: {
- type: "array"
- },
- object: {
- type: "object"
- },
- boolean: {
- type: "boolean"
- },
- null: {
- type: "null"
- },
- any: {
- type: "any"
- },
- undefined: {
- },
- stringnumber: {
- type: ["string","number"]
- },
- schema: {
- type: [{
- type: "string",
- minLength: "5"
- }]
- }
- }
- },
- valid: [{
- string: "hello",
- number: 1.50,
- integer: 1,
- boolean: true,
- array: [],
- object: {},
- null: null,
- any: "test",
- undefined: "test",
- stringnumber: "a",
- schema: "abcdef"
- },{
- stringnumber: 5
- }],
- invalid: [{
- string: 1
- },{
- number: "test"
- },{
- integer: 1.5
- },{
- integer: "test"
- },{
- boolean: "true"
- },{
- array: {}
- },{
- object: []
- },{
- null: {}
- },{
- stringnumber: true
- },{
- schema: "abc"
- },{
- schema: 5
- }]
- },
- multipleOf: {
- schema: {
- multipleOf: 5
- },
- valid: [5,0,10],
- invalid: [5.5,8,1]
- },
- divisibleBy: {
- schema: {
- divisibleBy: 5
- },
- valid: [5,0,10],
- invalid: [5.5,8,1]
- },
- multipleOfDecimal: {
- schema: {
- multipleOf: .01
- },
- valid: [1,1.14,3.57,56],
- invalid: [1.012]
- },
- minmax: {
- schema: {
- type: "object",
- properties: {
- min: {
- minimum: 5
- },
- exmin: {
- minimum: 5,
- exclusiveMinimum: true
- },
- max: {
- maximum: 5
- },
- exmax: {
- maximum: 5,
- exclusiveMaximum: true
- }
- }
- },
- valid: [{
- min: 5,
- exmin: 6,
- max: 5,
- exmax: 4
- }],
- invalid: [{min: 4},{exmin:5},{max:6},{exmax:5}]
- },
- minmaxLength: {
- schema: {
- type: "object",
- properties: {
- min: {
- minLength: 5
- },
- max: {
- maxLength: 5
- }
- }
- },
- valid: [{
- min: "abcde",
- max: "abcde"
- },{
- min: "abcdef",
- max: "abcd"
- }],
- invalid: [{min: "abcd"},{max:"abcdef"}]
- },
- pattern: {
- schema: {
- pattern: "[0-9]"
- },
- valid: ["abc123"],
- invalid: ["abc"]
- },
- items_schema: {
- schema: {
- items: {
- type: "string"
- }
- },
- valid: [
- ["test",""],
- []
- ],
- invalid: [
- ["test",123]
- ]
- },
- items_array: {
- schema: {
- items: [{
- type: "string"
- },{
- type: "number"
- }],
- additionalItems: false
- },
- valid: [
- ["test",123]
- ],
- invalid: [
- ["test",123,123],
- [123,"test"]
- ]
- },
- additionalItems: {
- schema: {
- items: [{
- type: "string"
- }],
- additionalItems: {
- type: "number"
- }
- },
- valid: [
- ["test",1],
- ["test"]
- ],
- invalid: [
- ["test",1,"test"],
- ["test","test2"]
- ]
- },
- minmaxItems: {
- schema: {
- type: "object",
- properties: {
- min: {
- minItems: 3
- },
- max: {
- maxItems: 3
- }
- }
- },
- valid: [
- {
- min: [1,2,3],
- max: [1,2,3]
- },
- {
- min: [1,2,3,4],
- max: [1,2]
- }
- ],
- invalid: [
- {
- min: [1,2]
- },
- {
- max: [1,2,3,4]
- }
- ]
- },
- uniqueItems: {
- schema: {
- uniqueItems: true
- },
- valid: [[1,2,3,{a:1},"1","{a:1}"]],
- invalid: [
- [1,1,2],
- [{a: 1},{a: 1}]
- ]
- },
- minmaxProperties: {
- schema: {
- type: "object",
- properties: {
- min: {
- minProperties: 2
- },
- max: {
- maxProperties: 2
- }
- }
- },
- valid: [
- {
- min: {
- a: 1,
- b: 2
- },
- max: {
- a: 1,
- b: 2
- }
- },
- {
- min: {
- a: 1,
- b: 2,
- c: 3
- },
- max: {
- a: 1
- }
- }
- ],
- invalid: [
- {
- min: {
- a: 1
- }
- },
- {
- max: {
- a: 1,
- b: 2,
- c: 3
- }
- }
- ]
- },
- required4: {
- schema: {
- type: "object",
- properties: {
- required: {}
- },
- required: ["required"],
- additionalProperties: true
- },
- valid: [{required: true, test: 1}],
- invalid: [
- {}
- ]
- },
- required4_merge: {
- schema: {
- type: "object",
- properties: {
- orig: {},
- other: {}
- },
- required: ["orig"],
- oneOf: [
- {
- required: ["other"]
- }
- ]
- },
- valid: [
- {
- orig: true,
- other: true
- }
- ],
- invalid: [
- {
- orig: true
- },
- {
- other: true
- }
- ]
- },
- properties: {
- schema: {
- type: "object",
- properties: {
- required: {
- type: "string"
- }
- },
- additionalProperties: {
- type: "number"
- }
- },
- valid: [{required: "test"},{other: 1}],
- invalid: [
- {other: "test"},
- {required: 1}
- ]
- },
- patternProperties: {
- schema: {
- type: "object",
- patternProperties: {
- "^test": {
- type: "string"
- }
- },
- additionalProperties: false
- },
- valid: [
- {testing: "test"},
- {}
- ],
- invalid: [
- {testing: 1},
- {other: "five"}
- ]
- },
- dependencies: {
- schema: {
- type: "object",
- dependencies: {
- property: ['property1','property2'],
- schema: {
- properties: {
- schema2: {
- type: "string"
- }
- }
- }
- }
- },
- valid: [
- {
- property: "",
- property1: "",
- property2: "",
- schema2: 1
- },
- {
- schema: ""
- },
- {
- schema: "",
- schema2: ""
- }
- ],
- invalid: [
- {
- property: "",
- property1: ""
- },
- {
- schema: "",
- schema2: 2
- }
- ]
- },
- definitions: {
- schema: {
- type: "object",
- properties: {
- storage: {
- $ref: "#/definitions/diskDevice"
- },
- external: {
- $ref: "#/definitions/external"
- }
- },
- definitions: {
- diskDevice: {
- pattern: "^/dev/[^/]+(/[^/]+)*$"
- },
- external: {
- "$ref": "../fixtures/string.json"
- }
- }
- },
- valid: [
- {
- storage: "/dev/test",
- external: "test"
- }
- ],
- invalid: [
- {
- storage: "test"
- },
- {
- external: 1
- }
- ]
- },
- definitions_nested: {
- schema: {
- type: "object",
- properties: {
- id: {
- $ref: "#/definitions/app/definitions/id"
- },
- definitions: {
- type: "number"
- },
- test: {
- $ref: "#/definitions/test"
- }
- },
- definitions: {
- app: {
- definitions: {
- id: {
- type: "string"
- }
- }
- },
- test: {
- properties: {
- definitions: {
- type: "number"
- }
- }
- }
- }
- },
- valid: [
- {
- id: "test",
- definitions: 3,
- test: {definitions: 3}
- }
- ],
- invalid: [
- {
- id: 1
- },
- {
- definitions: "string"
- },
- {
- test: {definitions: "string"}
- }
- ]
- },
- $ref: {
- schema: {
- type: "object",
- properties: {
- "/": { $ref: '../fixtures/string.json' }
- }
- },
- valid: [
- {
- "/": "test"
- }
- ],
- invalid: [
- {
- "/": 1
- }
- ]
- },
- $ref_recursive: {
- schema: {
- type: "object",
- properties: {
- "recursive": { $ref: '../fixtures/recursive.json' },
- "string": {
- $ref: "../fixtures/string.json"
- }
- }
- },
- valid: [
- {
- "recursive": {
- "string": "test"
- },
- "string": "test"
- }
- ],
- invalid: [
- {
- "recursive": "test"
- },
- {
- "recursive": {
- "string": 1
- }
- },
- {
- "string": 1
- }
- ]
- },
- disallow: {
- schema: {
- type: "object",
- properties: {
- union: {
- disallow: [
- "string",
- {
- type: "number",
- maximum: 5
- }
- ]
- },
- simple: {
- disallow: "number"
- }
- }
- },
- valid: [
- {
- union: 7,
- simple: "test"
- },
- {
- union: [],
- simple: {}
- }
- ],
- invalid: [
- {
- union: 4
- },
- {
- union: "test"
- },
- {
- simple: 3
- }
- ]
- },
- custom_validation: {
- schema: {
- type: "string",
- format: "date"
- },
- valid: ["1999-01-01"],
- invalid: ["1999","abc","abc 1999-01-01","1999-01-01 abc"]
- }
- };
- // Custom validators must return an array of errors or an empty array if valid
- $.jsoneditor.custom_validators.push(function(schema, value, path) {
- var errors = [];
- if(schema.format==="date") {
- if(!/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(value)) {
- // Errors must be an object with `path`, `property`, and `mesage`
- errors.push({
- path: path,
- property: 'format',
- message: 'Dates must be in the format "YYYY-MM-DD"'
- });
- }
- }
- return errors;
- });
- var num = 0;
- var animel = $("<div></div>");
- $.each(tests,function(i,test) {
- animel.queue(function(next) {
- console.log(i);
- try {
- var editor = new JSONEditor(document.createElement('div'),{
- schema: test.schema,
- ajax: true
- });
- }
- catch(e) {
- console.log(test.schema);
- throw e;
- }
- editor.on('ready',function() {
- $.each(test.valid,function(j,value) {
- try {
- num++;
- var result = editor.validate(value);
- if(result.length) {
- console.error(num,'valid',j,JSON.stringify(result,null,2));
- $("#output").append("<div><strong>"+i+" test "+j+"</strong>: Fail. Expected: [], Actual: "+JSON.stringify(result)+"</div>");
- }
- else {
- $("#output").append("<div><strong>"+i+" test "+j+"</strong>: Test for valid is success</div>");
- console.log(num,'valid',j);
- }
- }
- catch(e) {
- console.log(test.schema,value);
- throw e;
- }
- });
- $.each(test.invalid,function(j,value) {
- try {
- num++;
- var result = editor.validate(value);
- if(!result.length) {
- console.error(num,'invalid',j,JSON.stringify(result,null,2));
- $("#output").append("<div><strong>"+i+" test "+j+"</strong>: Fail. Expected: errors, Actual: []</div>");
- }
- else {
- var errors = [];
- $.each(result,function(k,error) {
- errors.push(error.path+": "+error.message);
- });
- if(errors.length === 1) errors = errors[0];
- $("#output").append("<div><strong>"+i+" test "+j+"</strong>: Test for invalid is success</div>");
- console.log(num,'invalid',j,JSON.stringify(errors,null,2));
- }
- }
- catch(e) {
- console.log(test.schema,value);
- throw e;
- }
- });
- next();
- });
- });
- });
- </script>
- </body>
- </html>
|