2016-01-12 19:40:23 +08:00
<!DOCTYPE HTML>
2020-09-23 16:23:38 +08:00
< html lang = "en" >
2016-01-12 19:40:23 +08:00
< head >
2020-09-23 16:23:38 +08:00
< meta charset = "utf-8" >
2016-01-12 19:40:23 +08:00
< title > JSONEditor | JSON schema validation< / title >
< link href = "../dist/jsoneditor.css" rel = "stylesheet" type = "text/css" >
< script src = "../dist/jsoneditor.js" > < / script >
< style type = "text/css" >
body {
width: 600px;
font: 11pt sans-serif;
}
#jsoneditor {
width: 100%;
height: 500px;
}
2019-03-17 22:56:29 +08:00
/* custom bold styling for non-default JSON schema values */
.jsoneditor-is-not-default {
font-weight: bold;
}
2016-01-12 19:40:23 +08:00
< / style >
< / head >
< body >
< h1 > JSON schema validation< / h1 >
< p >
2019-03-17 22:56:29 +08:00
This example demonstrates JSON schema validation. The JSON object in this example must contain properties like < code > firstName< / code > and < code > lastName< / code > , can can optionally have a property < code > age< / code > which must be a positive integer.
2016-01-12 19:40:23 +08:00
< / p >
< p >
See < a href = "http://json-schema.org/" target = "_blank" > http://json-schema.org/< / a > for more information.
< / p >
< div id = "jsoneditor" > < / div >
< script >
2019-08-29 21:45:32 +08:00
const schema = {
2019-06-12 21:43:16 +08:00
"title": "Employee",
"description": "Object containing employee details",
2016-01-12 19:40:23 +08:00
"type": "object",
"properties": {
"firstName": {
2019-03-17 22:39:00 +08:00
"title": "First Name",
"description": "The given name.",
"examples": [
"John"
],
2016-01-12 19:40:23 +08:00
"type": "string"
},
"lastName": {
2019-03-17 22:39:00 +08:00
"title": "Last Name",
"description": "The family name.",
"examples": [
"Smith"
],
2016-01-12 19:40:23 +08:00
"type": "string"
},
2016-01-15 04:10:30 +08:00
"gender": {
2019-03-17 22:39:00 +08:00
"title": "Gender",
2016-01-15 04:10:30 +08:00
"enum": ["male", "female"]
},
2018-08-12 18:41:02 +08:00
"availableToHire": {
2019-03-17 22:39:00 +08:00
"type": "boolean",
"default": false
2018-08-12 18:41:02 +08:00
},
2016-01-12 19:40:23 +08:00
"age": {
"description": "Age in years",
"type": "integer",
2019-03-14 00:43:31 +08:00
"minimum": 0,
"examples": [28, 32]
2017-07-05 05:28:28 +08:00
},
"job": {
"$ref": "job"
2016-01-12 19:40:23 +08:00
}
},
"required": ["firstName", "lastName"]
2019-08-29 21:45:32 +08:00
}
2016-01-12 19:40:23 +08:00
2019-08-29 21:45:32 +08:00
const job = {
2017-07-05 05:28:28 +08:00
"title": "Job description",
"type": "object",
2018-08-12 18:41:02 +08:00
"required": ["address"],
2017-07-05 05:28:28 +08:00
"properties": {
"company": {
2019-03-17 22:39:00 +08:00
"type": "string",
"examples": [
"ACME",
"Dexter Industries"
]
2017-07-05 05:28:28 +08:00
},
"role": {
2019-03-17 22:39:00 +08:00
"description": "Job title.",
"type": "string",
"examples": [
"Human Resources Coordinator",
"Software Developer"
2019-03-17 22:56:29 +08:00
],
"default": "Software Developer"
2018-08-12 18:41:02 +08:00
},
"address": {
"type": "string"
},
"salary": {
"type": "number",
2019-03-14 00:43:31 +08:00
"minimum": 120,
"examples": [100, 110, 120]
2017-07-05 05:28:28 +08:00
}
}
2019-08-29 21:45:32 +08:00
}
2017-07-05 05:28:28 +08:00
2019-08-29 21:45:32 +08:00
const json = {
2016-01-12 19:40:23 +08:00
firstName: 'John',
lastName: 'Doe',
2016-01-15 04:10:30 +08:00
gender: null,
2018-08-12 18:41:02 +08:00
age: "28",
2019-03-17 22:56:29 +08:00
availableToHire: true,
2017-07-05 05:28:28 +08:00
job: {
company: 'freelance',
2018-08-12 18:41:02 +08:00
role: 'developer',
salary: 100
2017-07-05 05:28:28 +08:00
}
2019-08-29 21:45:32 +08:00
}
2016-01-12 19:40:23 +08:00
2019-08-29 21:45:32 +08:00
const options = {
2017-07-05 05:28:28 +08:00
schema: schema,
2018-08-12 18:41:02 +08:00
schemaRefs: {"job": job},
mode: 'tree',
2019-07-24 19:30:44 +08:00
modes: ['code', 'text', 'tree', 'preview']
2019-08-29 21:45:32 +08:00
}
2016-01-12 19:40:23 +08:00
// create the editor
2019-08-29 21:45:32 +08:00
const container = document.getElementById('jsoneditor')
const editor = new JSONEditor(container, options, json)
2016-01-12 19:40:23 +08:00
< / script >
< / body >
< / html >