This package has been deprecated

Author message:

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

alt-schema

1.1.3 • Public • Published

Alt JSON Schema

An alternate JSON schema specification and related utility methods to define schemas and to validate and shape JSON objects.

Background

The official JSON Schema and various JSON validation libraries are great but for most of my use cases, I found them pretty verbose. I was looking for a one line solution for my JSON schema verifications.

I came up with this schema initially as a way to catch UI errors when backend APIs changed, but later found it to be very handy for many other use cases as well. Think of "Alt JSON Schema" as a short hand for defining JSON schemas.

The fact that it is a shorthand, also means it is lossy in terms of use case coverage. It will not address every use case, but for most practical purposes this might suffice.

Alt JSON Schema Syntax

Alt JSON schema is a string only representation of schema of any JSON.

There are few basic elements that form a alt JSON schema:

  1. type: A type could be any custom or basic data-type. It could be user defined. It is identified by a string. Example: integer, boolean, etc.
  2. key:type pairs: A object (hash) schema can be thought of as a list of key:type pairs. Thus I could define {"foo":"bar"} using a schema "{foo:string}". A data-type that is also optional can be represented by prefixing it with a ?. Example: "{foo:?string}", which then would be a valid schema for both {} and {"foo":null}.
  3. type:example: A type:example pair can be used instead of just type when shaping JSON, where the example is the default value to fill missing attributes. Example: Shaping {} using "{foo:s:bar}" would provide a JSON object of {"foo":"bar"}.
  4. Objects / Hash: An object is represented using curly braces "{} consiting of key:type pairs separated by comma. Example: "{foo:string,bar:integer}"
  5. Arrays / Lists: An array is represented using square backet "[]" consisting of type, separated by comma. Multiple types in an Array is meant to represent Array's with different types at different indexes.

You can use the above elements to define the schema for any arbitrarily complex JSON object.

Note: Since alt JSON schema is a shorthand, the schemas will always validate against some variations of the object as well, that in few edge scenarios may be undesirable.

Alt JSON Schema Example

let json = {"a":"foo", "b":1, "c": [1,2], "d": {"e": null}}

// A possible schema for the above JSON
let schema = "{a:s, b:i, c:[i], d:{e:?}}";

// Since types can be custom defined, s=string, i=integer

// Spaces/newlines are to be ignored

Installation

npm install -s alt-schema

const {verify, check, shape, toAltSchema, addType } = require('alt-schema')

import { verify, check, shape, toAltSchema, addType } from 'alt-schema';

toAltSchema

Use this to build a schema automatically for any JSON object.

const {toAltSchema} = require('alt-schema');

let json = {"a":"foo", "b":1, "c": [1,2], "d": {"e": null}};

let schema = toAltSchema(json);

console.log(schema); 
// {a:s,b:i,c:[i],d:{e:?}} 
// See section - Built in types

verify

Verify any JSON object against a schema. This method throws an exception when validation fails.

const { verify } = require("alt-schema");

let schema = "{a:i,b:[i],c:?b}";
let object = {"a":1, "b":[1,2,3], "c": 10}
verify(object, schema); 
// Throws error: 'json.c: validation failed'

check

Same as verify except returns boolean instead of throwing error

const {check} = require("verify-json");

let schema = "{a:i,b:[i],c:?b}";
let object = {"a":1, "b":[1,2,3], "c": 10}
verify(object, schema); 
// false

shape

Returns an object in the shape of the schema, making best effort of using values from the data object.

const {shape} = require("verify-json");

let schema = "{a:i,b:[i],c:b}";
let object = {"a":1, "b":[1], "d": 1}
shape(object, schema); 
// {"a": 1, "b":[1], "c":true}

addType

Add custom type validators and optionally provide default values when shaping objects

const {shape, addType} = require("verify-json");

addType('url', (value) => {
  if (value === undefined) return 'https://example.com'; // shape sample
  return value.match(/^http/) ? true : false;
});

shape(null, "{img:url}");
// {"img": "https://example.com"}

Built in types

The following built in types are available for use:

addType(["string","s"], _.isString);
addType(["number","n"], _.isNumber);
addType(["boolean","b"], _.isBoolean);
addType(["integer","i"], _.isInteger);

// Note: An array for type name is to specify multiple names for same type.

License

MIT © SleekSky LLC

Package Sidebar

Install

npm i alt-schema

Weekly Downloads

1

Version

1.1.3

License

MIT

Unpacked Size

22.3 kB

Total Files

7

Last publish

Collaborators

  • ynzi