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

def-schema

v1.0.1

Published

Define Object schema and test or validate target object.

Downloads

3

Readme

def-schema

Define Object schema and test or validate target object.

Important: Only support ECMAScript 6, both node engine and browsers.

Install

  npm install def-schema

and


let def = require('def-schema');

Example: Basic


    def(String).match(123);   // return false
    def(String).match('123'); // return true
 
    def(String).validate(123); // throw IncompatibleTypeError if not match
    def(String).validate('123'); // passed with silence

def define a new Schema object. Use match to test type of target object, use validate to assert target object.

validate is as same as match except it will throw exception if not match.

Example: Object

  let Person = def({name:String, age:Number});

  Person.match({name:'foo', age: 123, otherPro:'111'});   //return true
  Person.match({age:123});     //return false, missing name
  Person.match({name: 123});   //return false, name is not string

Example: Function

  let Person = def({
    name: String,
    age: Number,
    sayHello: Function
  });

  let person = {
    name: 'test',
    age: 123,
    sayHello() {
      //...
    }
  };

  Person.match(person); // retrun true

Example: Array

  
  def([]).match([1,2,'test']);  //return true
  def([Number]).match([1,2,'test']); //return false
  def([Number]).match([1,2,3]); //return true
  def[Number,String].match([1,'test']); //return true
  def[Number,String].match([1,'test',4,5]); //return true

Example: Combine Array and Object

  let Person = def({
    name: String
    age: Number
    skills: [String]
  });

  let person1 = {name:'foo', age:123, skill: ['js', 'java']};
  let person2 = {name:'foo', age:123, skill: 'js'};

  Person.match(person1); //return true;
  Person.match(person2); //return false;

  def([Person]).match([person1, person2]); //return false;

Example: Customize validator

  let Integer = def.fn(x => def(Number).match(x) && value % 1 === 0 && value >= -2147483648 && value <= 2147483647);

  Integer.match(123); //return true;
  Integer.match(1.23); //return false;

Here, by define the validate function we can decided what is 'Integer' in our program.

Example: Nest definition

Defined new type by leverage existing type, I mean:

  let Integer = def.fn(x => def(Number).match(x) && value % 1 === 0 && value >= -2147483648 && value <= 2147483647);
  let Person = def({
    name: String,
    age: Integer
  });

  Person.match({name: 'foo', age:123}); //return true;
  Person.match({name: 'foo', age:1.23}); //return false;

});	

Example: Opertional property

  let Person = def({
    name: String,
    age: def.opt(Number)
  });

  Person.match({name:'foo', age:1234}); //return true;
  Person.match({name:'foo'}); //return true;
  Person.match({name:'foo', age:'bar'}); //return false;

Example: Other buildin functions

  def.or(String,Number).match('hello');  //return true;
  def.and({name:String},{age:Number}).match({name:'foo', age:123});  //return true;

  let proto = {name:String};
  let target = Object.create(proto);
  def.asProto(proto).match(target);  //return true;


  let Range = def.param( (x, from, to) => def(Number).match(x) && x >= from  && x <= to );

  Range(1,10).match(5); //return true;
  Range(1,10).match(-5); //return return false;

Example: Reuse definition partially

  let Person = def({
    name: {
      first: String,
      last: String
    },
    age: Number
  });

  Person.name.match({first:'foo', last:'bar'}); //return true
  Person.name.match({first:'foo'}); //return false

Example: Generate random instance

  let Person = def({
    name: String,
    age:Number
  })

  Person.random //return random instance which match definition of Person


Thanks :)