https://github.com/sumeetsarkar/ratifyjs
JSON schema validation library
https://github.com/sumeetsarkar/ratifyjs
javascript-library js json json-schema json-schema-validator validation
Last synced: 3 months ago
JSON representation
JSON schema validation library
- Host: GitHub
- URL: https://github.com/sumeetsarkar/ratifyjs
- Owner: sumeetsarkar
- License: mit
- Created: 2017-11-24T22:49:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-05T11:28:16.000Z (over 8 years ago)
- Last Synced: 2026-01-12T22:52:47.133Z (4 months ago)
- Topics: javascript-library, js, json, json-schema, json-schema-validator, validation
- Language: JavaScript
- Homepage: https://sumeetsarkar.github.io/ratifyjs/
- Size: 24.4 KB
- Stars: 12
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Ratify JS
> Ratify JS is a fast & small JSON schema validation library
### Installation
npm install ratify-js
### Apply Schema on Data
```js
const { ratify } = require('ratify-js');
// Usage 1 - direct approach
ratify(schema, data);
// Usage 2 - curried approach
const schemaValidator = ratify(schema);
schemaValidator(data);
```
### Define schema
```js
const { Types, } = require('ratify-js');
module.exports = {
type: Types.Object, /* Main Parent type: Object or Array */
fields: { /* fields of Object */
id: {
type: Types.Number, /* type: Number */
required: true, /* Required: true, or else considered false */
},
firstName: {
type: Types.String, /* type: String */
required: true,
test: /^[a-zA-Z]{2,12}/, /* test: assign regex */
},
lastName: {
type: Types.String,
default: '', /* default assignment, only if Required: false */
},
age: {
type: Types.Number,
required: true,
range: { /* Assign range -> min, max (for type: Number) */
min: 18,
},
},
hobbies: {
type: Types.Array, /* Type: Array */
length: { /* Assign length -> min, max (for type: Array) */
min: 1,
max: 5,
},
children: { /* chilren of array */
type: Types.String,
},
},
sports: {
type: Types.Array,
default: [], /* default assignment for array */
children: {
type: Types.String,
},
},
address: {
type: Types.Object, /* type: Object */
fields: {
street: {
type: Types.String,
required: true,
},
city: {
type: Types.String,
required: true,
},
state: {
type: Types.String,
required: true,
},
pin: {
type: Types.String,
},
},
},
},
};
```
### A sample JSON conforming to the above schema
```js
module.exports = {
id: 123456,
firstName: 'John',
lastName: 'Doe',
age: 25,
hobbies: ['reading', 'cinema'],
sports: ['volley-ball', 'badminton'],
address: {
street: '890 Fifth Avenue, Manhattan',
city: 'New York City',
state: 'New York',
pin: '',
},
};
```