jsoneditor/examples/07_json_schema_validation.html

139 lines
3.0 KiB
HTML
Raw Permalink Normal View History

2016-01-12 19:40:23 +08:00
<!DOCTYPE HTML>
<html lang="en">
2016-01-12 19:40:23 +08:00
<head>
<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;
}
/* 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>
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>
const schema = {
"title": "Employee",
"description": "Object containing employee details",
2016-01-12 19:40:23 +08:00
"type": "object",
"properties": {
"firstName": {
"title": "First Name",
"description": "The given name.",
"examples": [
"John"
],
2016-01-12 19:40:23 +08:00
"type": "string"
},
"lastName": {
"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": {
"title": "Gender",
2016-01-15 04:10:30 +08:00
"enum": ["male", "female"]
},
"availableToHire": {
"type": "boolean",
"default": false
},
2016-01-12 19:40:23 +08:00
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0,
"examples": [28, 32]
},
"job": {
"$ref": "job"
2016-01-12 19:40:23 +08:00
}
},
"required": ["firstName", "lastName"]
}
2016-01-12 19:40:23 +08:00
const job = {
"title": "Job description",
"type": "object",
"required": ["address"],
"properties": {
"company": {
"type": "string",
"examples": [
"ACME",
"Dexter Industries"
]
},
"role": {
"description": "Job title.",
"type": "string",
"examples": [
"Human Resources Coordinator",
"Software Developer"
],
"default": "Software Developer"
},
"address": {
"type": "string"
},
"salary": {
"type": "number",
"minimum": 120,
"examples": [100, 110, 120]
}
}
}
const json = {
2016-01-12 19:40:23 +08:00
firstName: 'John',
lastName: 'Doe',
2016-01-15 04:10:30 +08:00
gender: null,
age: "28",
availableToHire: true,
job: {
company: 'freelance',
role: 'developer',
salary: 100
}
}
2016-01-12 19:40:23 +08:00
const options = {
schema: schema,
schemaRefs: {"job": job},
mode: 'tree',
modes: ['code', 'text', 'tree', 'preview']
}
2016-01-12 19:40:23 +08:00
// create the editor
const container = document.getElementById('jsoneditor')
const editor = new JSONEditor(container, options, json)
2016-01-12 19:40:23 +08:00
</script>
</body>
</html>