forked from json-editor/json-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
describedby.html
161 lines (149 loc) · 5.08 KB
/
describedby.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<!DOCTYPE html>
<html>
<head>
<title>Hyper-Link DescribedBy JSON Editor Example</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Enable responsive viewport -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- jQuery -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<!-- Bootstrap4 -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<!-- fontawesome5 -->
<link rel='stylesheet' href='https://use.fontawesome.com/releases/v5.12.1/css/all.css'>
<!-- JSON-Editor -->
<script src="https://cdn.jsdelivr.net/npm/@json-editor/json-editor@latest/dist/jsoneditor.min.js"></script>
<style type="text/css">
body {
margin: 1em;
}
</style>
</head>
<body>
<h1>Hyper-Link DescribedBy JSON Editor Example</h1>
<div id='editor_holder'></div>
<button id='submit'>Submit (console.log)</button>
<script>
// Initialize the editor
var editor = new JSONEditor(document.getElementById("editor_holder"), {
theme: "bootstrap4",
iconlib: "fontawesome5",
disable_collapse: true,
disable_edit_json: true,
disable_properties: true,
//no_additional_properties: true,
schema: {
"title": "Property for Sale",
"$ref": "#/definitions/single",
"definitions": {
"multiple": {
"type": "array",
"items": {
"$ref": "#/definitions/single"
}
},
"single": {
"id": "basic",
"type": "object",
"properties": {
"propertyName": {
"type": "string",
"title": "Property Name",
"description": "The name of the property being sold",
"minLength": 5
},
"propertyType": {
"type": "string",
"title": "Property Type",
"enum": ["house", "car", "yacht","multiple"]
},
"propertyTypeOptions": {
"type": ["object","array"],
"title": "Property Options",
"watch": {
"tag": "basic.propertyType"
},
"links": [{
"rel": "describedBy",
"href": "#/definitions/{{tag}}"
}]
}
},
"required": ["propertyName","propertyType"],
},
"house": {
"properties": {
"address": {
"type": "string",
"title": "Address"
},
"rooms": {
"type": "integer",
"title": "Number Of Rooms"
}
}
},
"car": {
"properties": {
"brand": {
"type": "string",
"title": "Brand",
"enum": ["Audi", "BMW", "Chevrolet", "Opel", "Mitsubishi"]
},
"color": {
"type": "string",
"title": "Color",
"format": "color"
},
"year": {
"type": "string",
"title": "Build Year"
}
}
},
"yacht": {
"properties": {
"name": {
"type": "string",
"title": "Name"
},
"model": {
"type": "string",
"title": "Model"
},
"year": {
"type": "integer",
"title": "Year"
},
"yachtLength": {
"type": "integer",
"title": "Length"
}
}
}
}
}
});
function validate() {
var errors = editor.validate();
console.log(editor.getValue());
if(errors.length) {
// errors is an array of objects, each with a `path`, `property`, and `message` parameter
// `property` is the schema keyword that triggered the validation error (e.g. "minLength")
// `path` is a dot separated path into the JSON object (e.g. "root.path.to.field")
editor.root.showValidationErrors(errors);
console.log('ERRORS', errors);
}
else {
// It's valid!
console.log("OK");
}
}
// Hook up the submit button to log to the console
document.getElementById('submit').addEventListener('click',function() {
// Get the value from the editor
validate();
});
</script>
</body>
</html>