@aminnairi/jsonvalidator
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@aminnairi/jsonvalidator

A sane validator for your insane JSON data

NPM package version TypeScript typing included status Code coverage in percent minified + gzipped package size tree-shaking support status vulnerabilities count

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
    ]
  }
]

Back to summary

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);
}

Back to summary

TypeScript

import {validate, string} from "https://unpkg.com/@aminnairi/jsonvalidator/index.ts";

const validation = validate(string, null);

if (validation.error) {
  console.error(validation.error):
}

Back to summary

Node

$ npm install @aminnairi/jsonvalidator

Back to summary

ECMAScript

import jsonvalidator from "@aminnairi/jsonvalidator";

const {validate, string} = jsonvalidator;

const validation = validate(string, null);

if (validation.error) {
  console.error(validation.error);
}

Back to summary

CommonJS

"use strict";

const {validate, string} = require("@aminnairi/jsonvalidator");

const validation = validate(string, null);

if (validation.error) {
  console.error(validation.error);
}

Back to summary

TypeScript

import {validate, string} from "@aminnairi/jsonvalidator";

const validation = validate(string, null);

if (validation.error) {
  console.error(validation.error);
}

Back to summary

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>

Back to summary

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>

Back to summary

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);
}

Back to summary

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);
}

Back to summary

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);
}

Back to summary

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);
}

Back to summary

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);
}

Back to summary

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);
}

Back to summary

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);
}

Back to summary

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);
}

Back to summary

Changelog

See CHANGELOG.md.

Back to summary

Contributing

See CONTRIBUGING.md.

Back to summary

License

See LICENSE.

Back to summary

Readme

Keywords

Package Sidebar

Install

npm i @aminnairi/jsonvalidator

Weekly Downloads

6

Version

1.0.0

License

GPL-3.0-or-later

Unpacked Size

62.4 kB

Total Files

8

Last publish

Collaborators

  • aminnairi