forked from geraintluff/tv4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
182 lines (158 loc) · 5.15 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<html>
<head>
<title>Tiny Validator for v4 JSON Schema</title>
<style>
body {
background-color: #F0F0E0;
font-family: sans-serif;
}
a {
color: #04D;
text-decoration: none;
}
a:hover {
color: #08F;
text-decoration: underline;
}
#main {
width: 700px;
margin-left: auto;
margin-right: auto;
border: 1px solid #888;
border-radius: 3px;
background-color: #FFF;
}
h1 {
text-align: center;
font-size: 1.3em;
font-weight: bold;
border-bottom: 1px solid black;
margin: 0;
padding: 0.3em;
padding-left: 1em;
background-color: #F8F8F0;
border-top-left-radius: 3px;
border-top-right-radius: 3px;
}
.content {
padding-left: 1em;
padding-right: 1em;
}
.box {
font-size: 0.9em;
margin: 1em;
padding: 0.5em;
padding-left: 0.7em;
border: 1px solid black;
border-radius: 3px;
background-color: #F0F0E8;
}
code, .code {
background-color: #F8F8F8;
border: 1px solid #DDD;
border-radius: 3px;
}
</style>
</head>
<body>
<script src="tv4.js"></script>
<script>
function runDemo(elementId) {
var element = document.getElementById(elementId);
var text = "";
for (var i = 0; i < element.childNodes.length; i++) {
if (element.childNodes[i].nodeType == 3) {
text += element.childNodes[i].nodeValue;
}
}
eval(text);
}
</script>
<div id="main">
<h1>Tiny Validator for v4 JSON Schema</h1>
<div class="content">
<div class="box">
Download <a href="tv4.js">full</a> (17kb) or <a href="tv4.min.js">minified</a> (8.3kb, 2.86kb gzipped)
</div>
<p>Tiny Validator is a JavaScript library for validation against v4 the <a href="http://json-schema.org/">JSON Schema</a> standard.
<p>It supports <code>$ref</code> with JSON Pointer fragment paths (e.g. <code>http://example.com/schema#/properties/myKey</code>).
<p>It uses relatively modern JavaScript features (such as <code>JSON.stringify</code> and <code>Array.isArray</code>) so older browsers will need a compatability shim - <a href="https://github.com/kriskowal/es5-shim">this</a> is a good one.
<p>For the source code, see the <a href="https://github.com/geraintluff/tv4">GitHub repository</a>.
<h2>Usage:</h2>
<pre class="code">var valid = tv4.validate(data, schema);</pre>
<p>If validation fails, there will be an object describing the error in <code>tv4.error</code>. If there are any schemas missing (<code>$ref</code>s not resolved), then they will be present in <code>tv4.missing</code>.
<p>Missing schemas are treated as empty.
<h2>Demos:</h2>
<h3>Basic usage</h3>
<div class="content">
<a href="javascript:runDemo('demo1');">run demo</a>
<pre class="code" id="demo1">
var schema = {
"items": {
"type": "boolean"
}
};
var data1 = [true, false];
var data2 = [true, 123];
alert("data 1: " + tv4.validate(data1, schema)); // true
alert("data 2: " + tv4.validate(data2, schema)); // false
alert("data 2 error: " + JSON.stringify(tv4.error, null, 4));
</pre>
</div>
<h3>Use of <code>$ref</code></h3>
<div class="content">
<a href="javascript:runDemo('demo2');">run demo</a>
<pre class="code" id="demo2">
var schema = {
"type": "array",
"items": {"$ref": "#"}
};
var data1 = [[], [[]]];
var data2 = [[], [true, []]];
alert("data 1: " + tv4.validate(data1, schema)); // true
alert("data 2: " + tv4.validate(data2, schema)); // false
</pre>
</div>
<h3>Missing schema</h3>
<div class="content">
<a href="javascript:runDemo('demo3');">run demo</a>
<pre class="code" id="demo3">
var schema = {
"type": "array",
"items": {"$ref": "http://example.com/schema"}
};
var data = [1, 2, 3];
alert("Valid: " + tv4.validate(data, schema)); // true
alert("Missing schemas: " + JSON.stringify(tv4.missing));
</pre>
</div>
<h3>Referencing remote schema</h3>
<div class="content">
<a href="javascript:runDemo('demo4');">run demo</a>
<pre class="code" id="demo4">
tv4.addSchema("http://example.com/schema", {
"definitions": {
"arrayItem": {"type": "boolean"}
}
});
var schema = {
"type": "array",
"items": {"$ref": "http://example.com/schema#/definitions/arrayItem"}
};
var data1 = [true, false, true];
var data2 = [1, 2, 3];
alert("data 1: " + tv4.validate(data1, schema)); // true
alert("data 2: " + tv4.validate(data2, schema)); // false
</pre>
</div>
<h2>Asynchronous usage:</h2>
<p>Asynchronous usage (fetching schemas using AJAX) requires an additional file. Currently, the only version available relies on jQuery, but it is short and should be easily modifiable for other libraries: <a href="tv4.async-jquery.js">tv4.async-jquery.js</a></p>
<p>Asynchronous mode is signified by providing a callback as a third parameter:</p>
<pre class="code">tv4.validate(data, schema, function (isValid, validationError) { ... });</pre>
<p>The value of <code>validationError</code> is taken from <code>tv4.error</code>.
<h2>Tests:</h2>
<p>There are some <a href="tests/?all">tests</a>, but they use PHP to run, so they won't work when hosted by GitHub.
</div>
</div>
</body>
</html>