A sane validator for your insane JSON data
A sane validator for your insane JSON data
     
- Usage
- Installation
- Deno
- ECMAScript Module
- TypeScript
- Node
- ECMAScript
- CommonJS
- TypeScript
- Browser
- Script
- ECMAScript Module
- API
- validate
- string
- number
- boolean
- nil
- array
- object
- oneOf
- Changelog
- Contributing
- License
``typescript
import {validate, array, object, property, string, number} from "@aminnairi/jsonvalidator";
const schema = array(object([
property("id", number),
property("languages", array(string))
]));
const data = [
{id: 1, languages: ["javascript", "php", "python", "ruby"]},
{id: 2, languages: ["haskell", "purescript", "elm", null]}
];
const validation = validate(schema, data);
if (validation.error) {
console.error(validation.error);
}
`
`console`
expected null to be of type "string", at index 3 of [
"haskell",
"purescript",
"elm",
null
], for property "languages" of {
"id": 2,
"languages": [
"haskell",
"purescript",
"elm",
null
]
}, at index 1 of [
{
"id": 1,
"languages": [
"javascript",
"php",
"python",
"ruby"
]
},
{
"id": 2,
"languages": [
"haskell",
"purescript",
"elm",
null
]
}
]
#### ECMAScript Module
`typescript
import {validate, string} from "https://unpkg.com/@aminnairi/jsonvalidator?module";
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
`
#### TypeScript
`typescript
import {validate, string} from "https://unpkg.com/@aminnairi/jsonvalidator/index.ts";
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error):
}
`
`console`
$ npm install @aminnairi/jsonvalidator
#### ECMAScript
`typescript
import jsonvalidator from "@aminnairi/jsonvalidator";
const {validate, string} = jsonvalidator;
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
`
#### CommonJS
`javascript
"use strict";
const {validate, string} = require("@aminnairi/jsonvalidator");
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
`
#### TypeScript
`typescript
import {validate, string} from "@aminnairi/jsonvalidator";
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
`
#### Script
`html`
#### ECMAScript Module
`html`
Check that an input is valid according to a given schema. Throws an error if it does not.
`typescript
export interface SucceededValidation {
error: false
}
export interface FailedValidation {
error: string;
}
export type Validation
= SucceededValidation
| FailedValidation
export type Schema
= StringSchema
| NumberSchema
| BooleanSchema
| NullSchema
| ArraySchema
| ObjectSchema
| OneOfSchema
export const validate = (schema: Schema, input: unknown): Validation;
`
`typescript
import {validate, number} from "@aminnairi/jsonvalidator";
const schema = number;
const validation = validate(schema, "123");
if (validation.error) {
console.error(validation.error);
}
`
Schema for a string.
`typescript
export interface StringSchema {
type: "string";
}
export const string: StringSchema;
`
`typescript
import {validate, string} from "@aminnairi/jsonvalidator";
const schema = string;
const validation = validate(schema, "string");
if (validation.error) {
console.error(validation.error);
}
`
Schema for a number.
`typescript
export interface NumberSchema {
type: "number";
}
export const number: NumberSchema;
`
`typescript
import {validate, number} from "@aminnairi/jsonvalidator";
const schema = number;
const validation = validate(schema, 123);
if (validation.error) {
console.error(validation.error);
}
`
Schema for a boolean.
`typescript
export interface BooleanSchema {
type: "boolean";
}
export const boolean: BooleanSchema;
`
`typescript
import {validate, boolean} from "@aminnairi/jsonvalidator";
const schema = boolean;
const validation = validate(schema, true);
if (validation.error) {
console.error(validation.error);
}
`
Schema for null values.
`typescript
export interface NullSchema {
type: "null";
}
export const nil: NullSchema = {
`
`typescript
import {validate, nil} from "@aminnairi/jsonvalidator";
const schema = nil;
const validation = validate(schema, null);
if (validation.error) {
console.error(validation.error);
}
`
Schema for an array. It accepts either another schema or an array of index schema. An index schema is a special schema that only works for arrays and can validate data for a wanted array index.
`typescript
export interface IndexSchema {
type: "index";
index: number;
schema: Schema;
}
export interface ArraySchema {
type: "array";
values: Schema | Array
}
export const array = (schema: Schema | Array
export const index = (i: number, schema: Schema): IndexSchema;
`
`typescript
import {validate, array, number} from "@aminnairi/jsonvalidator";
const schema = array(number);
const validation = validate(schema, [123, 456]);
if (validation.error) {
console.error(validation.error);
}
`
`typescript
import {validate, array, index, number} from "@aminnairi/jsonvalidator";
const schema = array([index(0, string), index(1, number)]);
const validation = validate(schema, [123, 456]);
if (validation.error) {
console.error(validation.error);
}
`
Schema for an object. It accepts an array of property schema or optional property schema. A property schema is a special schema that allows the validation of a data for a wanted property. An optional property schema is similar to a property schema, except the data can be missing. A missing data is simply a data that is not present in the JSON data. Null values are not considered missing data.
`typescript
export interface PropertySchema {
type: "property";
key: string;
schema: Schema;
}
export interface OptionalPropertySchema {
type: "optionalProperty";
key: string;
schema: Schema;
}
export interface ObjectSchema {
type: "object";
properties: Array
}
export const object = (properties: Array
export const property = (key: string, schema: Schema): PropertySchema;
export const optionalProperty = (key: string, schema: Schema): OptionalPropertySchema;
`
`typescript
import {validate, object, property, number} from "@aminnairi/jsonvalidator";
const schema = object([property("x", number), property("y", number)]);
const validation = validate(schema, {x: 1, y: 2});
if (validation.error) {
console.error(validation.error);
}
`
`typescript
import {validate, object, optionalProperty, string} from "@aminnairi/jsonvalidator";
const schema = object([optionalProperty("token", string)]);
const validation = validate(schema, {});
if (validation.error) {
console.error(validation.error);
}
`
Schema for validating multiple possible schema for one data. It accepts an array of schema, excepted the special schemas mentioned above.
`typescript
export interface OneOfSchema {
type: "oneOf";
schemas: Array
}
export const oneOf = (schemas: Array
`
`typescript
import {validate, oneOf, number, string} from "@aminnairi/jsonvalidator";
const schema = oneOf([number, string]);
const validation = validate(schema, 123);
if (validation.error) {
console.error(validation.error);
}
`
See CHANGELOG.md.
See CONTRIBUGING.md.
See LICENSE`.