108 lines
2.2 KiB
HTML
108 lines
2.2 KiB
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<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;
|
|
}
|
|
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>JSON schema validation</h1>
|
|
<p>
|
|
This example demonstrates JSON schema validation. The JSON object in this example must contain properties <code>firstName</code> and <code>lastName</code>, can can optionally have a property <code>age</code> which must be a positive integer.
|
|
</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>
|
|
var schema = {
|
|
"title": "Example Schema",
|
|
"type": "object",
|
|
"properties": {
|
|
"firstName": {
|
|
"type": "string"
|
|
},
|
|
"lastName": {
|
|
"type": "string"
|
|
},
|
|
"gender": {
|
|
"enum": ["male", "female"]
|
|
},
|
|
"availableToHire": {
|
|
"type": "boolean"
|
|
},
|
|
"age": {
|
|
"description": "Age in years",
|
|
"type": "integer",
|
|
"minimum": 0
|
|
},
|
|
"job": {
|
|
"$ref": "job"
|
|
}
|
|
},
|
|
"required": ["firstName", "lastName"]
|
|
};
|
|
|
|
var job = {
|
|
"title": "Job description",
|
|
"type": "object",
|
|
"required": ["address"],
|
|
"properties": {
|
|
"company": {
|
|
"type": "string"
|
|
},
|
|
"role": {
|
|
"type": "string"
|
|
},
|
|
"address": {
|
|
"type": "string"
|
|
},
|
|
"salary": {
|
|
"type": "number",
|
|
"minimum": 120
|
|
}
|
|
}
|
|
};
|
|
|
|
var json = {
|
|
firstName: 'John',
|
|
lastName: 'Doe',
|
|
gender: null,
|
|
age: "28",
|
|
availableToHire: 1,
|
|
job: {
|
|
company: 'freelance',
|
|
role: 'developer',
|
|
salary: 100
|
|
}
|
|
};
|
|
|
|
var options = {
|
|
schema: schema,
|
|
schemaRefs: {"job": job},
|
|
mode: 'tree',
|
|
modes: ['code', 'text', 'tree']
|
|
};
|
|
|
|
// create the editor
|
|
var container = document.getElementById('jsoneditor');
|
|
var editor = new JSONEditor(container, options, json);
|
|
</script>
|
|
</body>
|
|
</html>
|