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

ht-schema

v3.4.1

Published

ht-schema

Downloads

105

Readme

ht-schema

Build Status Coverage Status

Every API function in HT requires a schema that defines the kind of data that function takes. While this may seem laborious at first, HT schemas are very pragmatic, combine both validation and coersion in one. This means you are free to assume that your data is valid and properly typed every time, no more manual validation or type casting required.

Schemas are inherently composible, you can nest schemas inside schemas, infact this is what happens when you add a String attribute to an Object schema anyway.

Schemas are also extensible, the default ones are defined in lib/schema.js and they are quite easy to read and create your own.

Here is an example schema:

var tagSchema = s.Object({
    id:    MyCustomValidator(),
    label: s.String()
});

var movieSchema = s.Object({
    releaseDate: s.Date({ min: '1900', max: new Date() }),
    runningTime: s.Number({ min: 0, max: 240 }),
    director:    s.String(),
    genre:       s.String({ enum: [ "Comedy", "Drama", "Action" ] }),
    tags:        s.Array([ tagSchema ]),
    extraInfo:   s.Object({ opt: true, strict: false })
});

Built-in Schema types and their default attributes:

s.Object { opt: false, strict: true }

An object validator with one string attribute:

s.Object({ name: s.String()});

A liberal object validator that can have any attribute:

s.Object({strict: false});

A liberal object validator that can have any attribute but specifies that a cat attribute must be an object with a name:

s.Object({strict: false}, { cat: s.Object({ name: s.String() }) });

An object validator that requires a foo attribute but remaps it to bar in output.

s.Object({ 'foo as bar': s.String() });

s.Object.keys.

You can access object keys through 'keys' on an instance of an object.


var schema = s.Object({
    hello: s.String()
});

schema.keys.hello.validate("world");

s.Number { opt: false }

s.String { opt: false, min: null, max: null, enum: null, trim: null, upper: null, lower: null, regex: null }

A string validator with an enum restriction

s.String({ enum: [ "apples", "oranges" ] });

A string validator with a length limit

s.String({ len: 10  });
s.String({ max: 256 });
s.String({ min: 1   });

A string validator that coerces the value

s.String({ trim: true  });
s.String({ upper: true });
s.String({ lower: true });

A string validator that matches a regex (regex can optionally be passed in as a string)

s.String({ regex: /h[ae]llo world/ });

s.Array { opt: false, length: null, minLength: null, maxLength: null }

An array validator which can only contain Dates.

s.Array([ s.Date() ]);

An optional array validator which can contain cats and dogs.

Note: Array validators match in precidence left to right.

var catSchema = s.Object({ 
    name:     s.String(), 
    attitude: s.String({ enum: [ "Surly", "Dissinterested", "bemused" ] })
});

var dogSchema = s.Object({ 
    name:     s.String(), 
    attitude: s.String({ enum: [ "Excited", "Confused", "Happy" ]})
});

s.Array({ opt: true }, [ catSchema, dogSchema ]);

s.Boolean { opt: false, coerce: false }

var boolSchema = s.Boolean();
boolSchema.validate(true); // -> true
var maybeSchema = s.Boolean({ coerce: true });
maybeSchema.validate("truthy"); // -> true

If coerce is set, truthy and falsy values will be converted to true/false

s.Date { opt: false, min: null, max: null }

min and max need to be unix epoch timestamps

s.Email { opt: false, normalize: true }

If normalize is true (default) then the output email will be lowercased.

Schema Utility Functions

These functions are part of the schema instance you get back schema.clone etc.

clone(...args)

Clone creates a copy of the schema.

If you pass an object as an argument to clone, it will merge your object with the schemas 'args', allowing you to set new attributes.

(Object only) If you pass an array as an argument, it will use the array values as a whitelist, and return a new Object schema with only these keys.

Note: You can pass more than one value to this function, they will be evaluated left to right.

Replacing args:


var schema = s.String({ opt: false });

schema.validate(); // this will throw

var newSchema = schema.clone({ opt: true });

newSchema.validate(); // this won't

Trimming an object:


var schema = s.Object({
    a: s.Number(),
    b: s.Number()
});

schema.validate({
    a: 5,
    b: 5
});

var newSchema = schema.clone([ 'a' ]); // only return a

newSchema.validate({
    a: 5,
    b: 5 // < doesn't exist in this schema!
});