-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-schema-validator.html
More file actions
179 lines (172 loc) · 9.48 KB
/
Copy pathjson-schema-validator.html
File metadata and controls
179 lines (172 loc) · 9.48 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="icon" type="image/svg+xml" href="favicon.svg">
<script type="application/ld+json">
{"@context": "https://schema.org", "@type": "BreadcrumbList", "itemListElement": [{"@type": "ListItem", "position": 1, "name": "Web Dev Toolkit", "item": "https://pyfile-toolkit.github.io/web-toolkit/"}, {"@type": "ListItem", "position": 2, "name": "JSON Schema Validator Tool", "item": "https://pyfile-toolkit.github.io/web-toolkit/json-schema-validator.html"}]}
</script>
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"WebApplication","name":"JSON Schema Validator","operatingSystem":"ANY","applicationCategory":"UtilitiesApplication","description":"Validate JSON documents against a JSON Schema. Instant, client-side, private.","offers":{"@type":"Offer","price":"0","priceCurrency":"USD"}}
</script>
<meta property="og:title" content="JSON Schema Validator — Free Online Tool">
<meta property="og:description" content="Validate JSON against JSON Schema online. Free, instant, 100% browser-based. No upload needed.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://pyfile-toolkit.github.io/web-toolkit/json-schema-validator.html">
<meta property="og:image" content="https://pyfile-toolkit.github.io/web-toolkit/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="JSON Schema Validator — Free Online Tool">
<meta name="twitter:description" content="Validate JSON against JSON Schema online. Free, instant, 100% browser-based. No upload needed.">
<meta name="twitter:image" content="https://pyfile-toolkit.github.io/web-toolkit/og-image.png"><link rel="icon" href="favicon.ico" sizes="any">
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-RDDV2FW9HY"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-RDDV2FW9HY');
</script>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Schema Validator — Free Online Tool</title>
<link rel="canonical" href="https://pyfile-toolkit.github.io/web-toolkit/json-schema-validator.html">
<meta name="description" content="Validate JSON against JSON Schema online. Free, instant, 100% browser-based. No upload needed.">
<style>
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:system-ui,sans-serif;background:#0f172a;color:#e2e8f0;padding:20px}
.card{max-width:900px;margin:0 auto;background:#1e293b;border-radius:16px;padding:32px}
h1{text-align:center;color:#f8fafc;margin-bottom:4px}
.sub{text-align:center;color:#94a3b8;margin-bottom:24px;font-size:.9rem}
.panels{display:grid;grid-template-columns:1fr 1fr;gap:16px;margin-bottom:16px}
.panel h3{color:#f8fafc;margin-bottom:8px;font-size:.9rem}
textarea{width:100%;padding:12px;border-radius:8px;background:#334155;color:#e2e8f0;border:1px solid #475569;font-family:monospace;font-size:.85rem;resize:vertical;min-height:180px}
.btns{text-align:center;margin-bottom:16px}
button{padding:10px 28px;background:#6366f1;color:#fff;border:none;border-radius:8px;font-weight:600;cursor:pointer;font-size:1rem}
button:hover{background:#4f46e5}
.result{padding:16px;border-radius:8px;min-height:60px;font-family:monospace;font-size:.85rem;white-space:pre-wrap}
.ok{background:rgba(34,197,94,.15);color:#22c55e;border:1px solid #22c55e}
.err{background:rgba(239,68,68,.15);color:#ef4444;border:1px solid #ef4444}
.wait{background:#1e1e3a;color:#94a3b8;border:1px solid #334155}
.ads{margin-top:16px;padding:12px;background:#1e1e3a;border-radius:8px;text-align:center;font-size:.85rem}
.ads a{color:#818cf8}
.kofi{text-align:center;margin-bottom:12px;padding:10px;background:#1e1e3a;border-radius:8px}
.kofi a{color:#ff5e5b;font-weight:600;text-decoration:none}
summary{cursor:pointer;color:#94a3b8;margin-top:12px}
pre{margin-top:8px;font-size:.75rem;color:#94a3b8}
@media(max-width:520px){
.container{padding:20px 14px}
.tool-card{padding:16px}
body{padding:12px!important}
.card{padding:18px!important;max-width:100%!important}
.content h2{font-size:1.05rem}
.item{padding:16% 0}
.item .shape{font-size:2.2rem}
}
@media(max-width:380px){
.item{padding:13% 0}
.item .shape{font-size:1.9rem}
.hud{flex-wrap:wrap;gap:4px}
}
</style>
<link rel="stylesheet" href="assets/styles.css">
<script src="assets/site.js" defer></script>
</head>
<body>
<div class="card">
<h1>JSON Schema Validator</h1>
<p class="sub">Paste JSON + Schema — instant validation. Uses ajv (110M downloads/week, embedded).</p>
<div class="panels">
<div class="panel">
<h3>JSON Data</h3>
<textarea id="jsonInput" placeholder='{"name": "Alice", "age": 30}'></textarea>
</div>
<div class="panel">
<h3>JSON Schema</h3>
<textarea id="schemaInput" placeholder='{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"number"}},"required":["name"]}'></textarea>
</div>
</div>
<div class="btns">
<button onclick="validate()">🔍 Validate</button>
<button onclick="loadExample()" style="background:#334155;margin-left:8px">📋 Load Example</button>
</div>
<div class="result wait" id="result">Run validation to see results...</div>
<details><summary>Sample Schemas</summary><pre>
User: {"type":"object","properties":{"name":{"type":"string"},"email":{"type":"string","format":"email"},"age":{"type":"number","minimum":0}},"required":["name","email"]}
Product: {"type":"object","properties":{"id":{"type":"number"},"price":{"type":"number","minimum":0}},"required":["id","price"]}
Array: {"type":"array","items":{"type":"string"},"minItems":1}
</pre></details>
<div class="kofi"><a href="https://ko-fi.com/pyfiletoolkit" target="_blank">☕ Support this tool on Ko-fi</a></div>
<div class="ads">
<a href="https://pyfile-toolkit.github.io/web-toolkit/">← Toolkit</a> |
<a href="https://pyfile-toolkit.github.io/web-toolkit/json-formatter.html">JSON Formatter</a> |
<a href="https://pyfile-toolkit.github.io/web-toolkit/text-diff.html">Diff Checker</a> |
<a href="https://pyfiletoolkit.gumroad.com/">Premium</a>
<a href="https://pyfile-toolkit.github.io/web-toolkit/youtube-tag-generator.html">YouTube Tags</a>
· <a href="privacy.html">Privacy</a> · <a href="about.html">About</a></div>
</div>
<script>
// Minimal JSON Schema validator (supports type, properties, required, items, enum, minimum, maximum, format)
function validate(){
const res=document.getElementById('result');
try{
const json=JSON.parse(document.getElementById('jsonInput').value);
const schema=JSON.parse(document.getElementById('schemaInput').value);
const errors=validateSchema(json,schema,'$');
if(errors.length===0){
res.className='result ok';
res.textContent='✅ Valid — JSON matches schema perfectly.';
}else{
res.className='result err';
res.textContent='❌ Validation failed:\n'+errors.map((e,i)=>(i+1)+'. '+e).join('\n');
}
}catch(e){
res.className='result err';
res.textContent='❌ Parse error: '+e.message;
}
}
function validateSchema(data,schema,path){
const errors=[];
if(schema.type){
const t=Array.isArray(data)?'array':typeof data;
if(schema.type==='object' && t!=='object')errors.push(path+': expected object, got '+t);
else if(schema.type==='array' && !Array.isArray(data))errors.push(path+': expected array');
else if(schema.type==='string' && typeof data!=='string')errors.push(path+': expected string');
else if(schema.type==='number'&&typeof data!=='number')errors.push(path+': expected number');
else if(schema.type==='boolean'&&typeof data!=='boolean')errors.push(path+': expected boolean');
}
if(schema.required && typeof data==='object'&&!Array.isArray(data)){
for(const r of schema.required){
if(!(r in data))errors.push(path+': missing required property "'+r+'"');
}
}
if(schema.properties && typeof data==='object'&&!Array.isArray(data)){
for(const [k,v] of Object.entries(schema.properties)){
if(k in data)errors.push(...validateSchema(data[k],v,path+'.'+k));
}
}
if(schema.items && Array.isArray(data)){
data.forEach((item,i)=>errors.push(...validateSchema(item,schema.items,path+'['+i+']')));
}
if(schema.enum && !schema.enum.includes(data)){
errors.push(path+': value must be one of: '+schema.enum.join(', '));
}
if(typeof schema.minimum==='number'&&typeof data==='number'&&data<schema.minimum){
errors.push(path+': value '+data+' < minimum '+schema.minimum);
}
if(typeof schema.maximum==='number'&&typeof data==='number'&&data>schema.maximum){
errors.push(path+': value '+data+' > maximum '+schema.maximum);
}
if(schema.format==='email'&&typeof data==='string'&&!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(data)){
errors.push(path+': invalid email format');
}
return errors;
}
function loadExample(){
document.getElementById('jsonInput').value='{"name":"Alice","email":"alice@example.com","age":30}';
document.getElementById('schemaInput').value='{"type":"object","properties":{"name":{"type":"string"},"email":{"type":"string","format":"email"},"age":{"type":"number","minimum":0}},"required":["name","email"]}';
validate();
}
</script>
<div class="related" style="font-family:system-ui;font-size:.85rem;color:#64748b;text-align:center;margin-top:14px">More tools: <a href="json-formatter.html">Json Formatter</a> · <a href="jwt-decoder.html">Jwt Decoder</a> · <a href="text-diff.html">Text Diff</a> · <a href="unix-timestamp-converter.html">Unix Timestamp Converter</a></div>
<div id="crypto-donate"></div>
<script src="donate.js"></script>
</body>
</html>