npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aminnairi/jsonvalidator

v1.0.0

Published

A sane validator for your insane JSON data

Downloads

29

Readme

@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