@aminnairi/jsonvalidator
A sane validator for your insane JSON data
Summary
Usage
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);
}
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
]
}
]
Installation
Deno
ECMAScript Module
import {validate, string} from "https://unpkg.com/@aminnairi/jsonvalidator?module";
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
TypeScript
import {validate, string} from "https://unpkg.com/@aminnairi/jsonvalidator/index.ts";
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error):
}
Node
$ npm install @aminnairi/jsonvalidator
ECMAScript
import jsonvalidator from "@aminnairi/jsonvalidator";
const {validate, string} = jsonvalidator;
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
CommonJS
"use strict";
const {validate, string} = require("@aminnairi/jsonvalidator");
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
TypeScript
import {validate, string} from "@aminnairi/jsonvalidator";
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
Browser
Script
<!DOCTYPE html>
<html>
<body>
<script src="https://unpkg.com/@aminnairi/jsonvalidator"></script>
<script>
"use strict";
const {validate, string} = window.aminnairi.jsonvalidator;
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
</script>
</body>
</html>
ECMAScript Module
<!DOCTYPE html>
<html>
<body>
<script type="module">
import {validate, string} from "https://unpkg.com/@aminnairi/jsonvalidator?module";
const validation = validate(string, null);
if (validation.error) {
console.error(validation.error);
}
</script>
</body>
</html>
API
validate
Check that an input is valid according to a given schema. Throws an error if it does not.
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;
import {validate, number} from "@aminnairi/jsonvalidator";
const schema = number;
const validation = validate(schema, "123");
if (validation.error) {
console.error(validation.error);
}
string
Schema for a string.
export interface StringSchema {
type: "string";
}
export const string: StringSchema;
import {validate, string} from "@aminnairi/jsonvalidator";
const schema = string;
const validation = validate(schema, "string");
if (validation.error) {
console.error(validation.error);
}
number
Schema for a number.
export interface NumberSchema {
type: "number";
}
export const number: NumberSchema;
import {validate, number} from "@aminnairi/jsonvalidator";
const schema = number;
const validation = validate(schema, 123);
if (validation.error) {
console.error(validation.error);
}
boolean
Schema for a boolean.
export interface BooleanSchema {
type: "boolean";
}
export const boolean: BooleanSchema;
import {validate, boolean} from "@aminnairi/jsonvalidator";
const schema = boolean;
const validation = validate(schema, true);
if (validation.error) {
console.error(validation.error);
}
nil
Schema for null values.
export interface NullSchema {
type: "null";
}
export const nil: NullSchema = {
import {validate, nil} from "@aminnairi/jsonvalidator";
const schema = nil;
const validation = validate(schema, null);
if (validation.error) {
console.error(validation.error);
}
array
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.
export interface IndexSchema {
type: "index";
index: number;
schema: Schema;
}
export interface ArraySchema {
type: "array";
values: Schema | Array<IndexSchema>;
}
export const array = (schema: Schema | Array<IndexSchema>): ArraySchema;
export const index = (i: number, schema: Schema): IndexSchema;
import {validate, array, number} from "@aminnairi/jsonvalidator";
const schema = array(number);
const validation = validate(schema, [123, 456]);
if (validation.error) {
console.error(validation.error);
}
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);
}
object
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.
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<PropertySchema | OptionalPropertySchema>;
}
export const object = (properties: Array<PropertySchema | OptionalPropertySchema>): ObjectSchema;
export const property = (key: string, schema: Schema): PropertySchema;
export const optionalProperty = (key: string, schema: Schema): OptionalPropertySchema;
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);
}
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);
}
oneOf
Schema for validating multiple possible schema for one data. It accepts an array of schema, excepted the special schemas mentioned above.
export interface OneOfSchema {
type: "oneOf";
schemas: Array<Schema>;
}
export const oneOf = (schemas: Array<Schema>): OneOfSchema;
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);
}
Changelog
See CHANGELOG.md
.
Contributing
See CONTRIBUGING.md
.
License
See LICENSE
.