Get started with z-schema CDN
MIT licensed
Z-Schema is a lightweight library for validating JSON Schema against JSON data.
Tags:- JSON
- Schema
- Validator
Stable version
Copied!
How to start using z-schema CDN
<!DOCTYPE html>
<html>
<head>
<title>Get started with z-schema CDN - cdnhub.io</title>
<script src="https://cdn.cdnhub.io/z-schema/6.0.1/ZSchema-browser-min.js"></script>
<style>
.error {
color: red;
}
</style>
</head>
<body>
<form id="myForm">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<span class="error" id="nameError"></span>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<span class="error" id="emailError"></span>
<button type="submit">Submit</button>
</form>
<script>
const form = document.getElementById('myForm');
const nameInput = document.getElementById('name');
const nameError = document.getElementById('nameError');
const emailInput = document.getElementById('email');
const emailError = document.getElementById('emailError');
form.addEventListener('submit', async (event) => {
event.preventDefault();
const schema = {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"email": {
"type": "string",
"format": "email"
}
},
"required": ["name", "email"]
};
const result = await ZSchema.validate(form, schema);
if (result.errors.length > 0) {
result.errors.forEach((error) => {
const errorElement = document.createElement('span');
errorElement.textContent = error.message;
errorElement.className = 'error';
errorElement.id = `error-${error.path.join('-')}`;
const input = document.querySelector(`input[name="${error.path[0]}"]`);
input.parentNode.insertBefore(errorElement, input.nextSibling);
});
return;
}
// Form is valid, submit to the server or perform other actions
console.log('Form is valid');
});
</script>
</body>
</html>
Copied!
Copied!
Copied!
Copied!
Copied!
All versions
1.4.2
1.4.3
1.4.4
1.5.0
1.5.1
2.0.0
2.0.1
2.0.2
2.0.3
2.0.4
2.0.5
2.1.0
2.2.0
2.2.1
2.2.2
2.3.0
2.3.1
2.4.0
2.4.1
2.4.10
2.4.2
2.4.3
2.4.4
2.4.6
2.4.7
2.4.8
2.4.9
3.0.0
3.0.1
3.0.3
3.0.4
3.1.0
3.1.1
3.1.2
3.1.3
3.1.4
3.1.5
3.10.0
3.10.1
3.10.2
3.11.0
3.12.0
3.12.1
3.12.2
3.12.3
3.12.4
3.12.5
3.13.0
3.14.0
3.14.1
3.14.2
3.15.1
3.15.2
3.15.3
3.15.4
3.16.0
3.16.1
3.17.0
3.18.0
3.18.1
3.18.2
3.18.3
3.18.4
3.19.0
3.19.1
3.2.0
3.20.0
3.21.0
3.22.0
3.23.0
3.24.0
3.24.1
3.24.2
3.24.3
3.25.0
3.25.1
3.26.0
3.3.0
3.3.1
3.3.2
3.3.3
3.4.0
3.4.1
3.4.2
3.4.3
3.5.0
3.5.1
3.5.2
3.6.0
3.6.1
3.6.2
3.7.0
3.7.1
3.8.0
3.9.0
3.9.1
3.9.2
3.9.3
3.9.4
3.9.5
4.0.0
4.0.1
4.0.2
4.1.0
4.1.1
4.2.0
4.2.1
4.2.2
4.2.3
4.2.4
5.0.0
5.0.1
5.0.2
5.0.3
5.0.4
5.0.5
5.0.6
6.0.0
6.0.1
*** 6.0.2