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

schemerjs

v1.1.1

Published

Micro object schema and validation library

Downloads

4

Readme

SchemerJS

A simple database agnostic JavasScript object modeling and validation library.

Instalation

npm install --save schemerjs

Basic Example:

const Schema = require("schemerjs").Schema;
const rules = require("schemerjs").rules;

let User = new Schema({
  firstname: "string",
  lastname: "string",
  bio: {
    type: "string",
    optional: true,
    rules: [
      rules.max(150)
    ]
  }
});

let fakeUser = {
  firstname: "John",
  lastname: "Doe",
  bio: "A generic person"
}

let result = User.validate(fakeUser);

console.log(result) // {isValid: true, invalidProps: [], reasons: {}}

Schema Definition:

Each property definition takes the following syntax:

let example = new Schema({
  propName: {
    type: ["string"],
    optional: true,
    rules: [],
    subRules: []
  }
});

Where propName is the property to be validated. If the object being validated has a property not defined in the schema, the object is considered to be invalid.

If all you care about is the key name and type you can use the shorthand syntax like this:

let example = new Schema({
  keyName: "number"
});

Schema Property Constraints:

(The constraints that go inside of the the property definition)

type String

The type the property must be.

Valid types:
  • "string"
  • "number"
  • "boolean"
  • "array"
  • "object"
  • "date"
  • "math"
  • "regexp"
  • "json"
  • "error"

For a property of an unspecified type use "any". To specify an array of a certain type use the syntax: ["type"] (Note that the array is itself not a string).

You can also set the type to another SchemerJS schema:

  let SubSchema = new Schema({
    bar: {
      type: "string"
    }
  });

  let TopSchema = new Schema({
    foo: {
      type: SubSchema
    },
    arrayofsubschema: {
      type: [SubSchema]
    }
  });

optional Boolean

A boolean representing whether the property is needed for the object to be considered valid. defaults to false.

rules [functions]

An array of functions that return either a Boolean or an Error. The object being validated is considered to be invalid if the any of the functions return false or an Error. The error.message is listed as a reason for failure if specified. Each rule function is called as rule(propertyVal, proptertyDefinition.type)

Example:
let ruleFunc = function(val, type) {
    // type == "string" (not used)
    // returns an error if val !== "world"
    if (val !== "world") {
      return new Error("does not equal world");
    }
};

let Example = new Schema({
  hello: {
    type: "string",
    rules: [
      ruleFunc // Note the we do not call ruleFunc here i.e. don't do ruleFunc()
    ]
  }
});

let pass = Example.validate({hello: "world"}); // isValid == true
let fail = Example.validate({hello: "universe"}); // isValid == false

subRules [functions]

Sub-rules are like regular rules except they are run on each value of an array.

Schema Functions:

validate Schema.validate(object)

Returns an object with the following properties:

  • isValid Boolean
  • invalidProps ["string"]
  • reasons "Object" // error messages for each of the invalidProps

getRequiredProps Schema.getRequiredProps()

Returns an Array of all the props needed for an object validated with this schema definition to be considered valid.

Predefined rules:

SchemerJS comes with a few predefined rules for convenience.

  • min
  • max
  • regex

Usage:

const Schema = require("schemerjs").Schema;
const rules = require("schemerjs").rules;

let Example = new Schema({
  hello: {
    type: "string",
    rules: [
      rules.min(1), // We use () because these functions return another function
      rules.max(6),
      rules.regex(/world/)
    ]
  }
});

min min(num)

Takes a Number and returns a Function. The function that min returns can be passed a Number String or Array. With a Number the function returns an Error if the value the function is given is less than the number passed to min. With a String or an Array the function returns an Error if the the value.length the function is given is less than the number passed to min.

Usage:

const rules = require("schemerjs").rules;

let atLeastFive = rules.min(5);

let passNum = atLeastFive(7); // returns undefined
let failNum = atLeastFive(2); // returns an Error
let passStr = atLeastFive('abcdefg'); // returns undefined
let failStr = atLeastFive('abc'); //returns an Error
let passArr = atLeastFive([1,2,3,4,5,6])// returns undefined
let failArr = atLeastFive([1,2,3])// returns an Error

max max(num)

Takes a Number and returns a Function. The function that max returns can be passed a Number String or Array. With a Number the function returns an Error if the value the function is given is greater than the number passed to max. With a String or an Array the function returns an Error if the the value.length the function is given is greater than the number passed to max.

Usage:

const rules = require("schemerjs").rules;

let atMostFive = rules.max(5);

let passNum = atMostFive(2); // returns undefined
let failNum = atMostFive(7); // returns an Error
let passStr = atMostFive('abc'); // returns undefined
let failStr = atMostFive('abcdefg'); //returns an Error
let passArr = atMostFive([1,2,4])// returns undefined
let failArr = atMostFive([1,2,3,4,5,6])// returns an Error

regex regex(regExp)

Takes a regular expression and returns a Function. The function that regex returns can be passed a String. If this String does not pass the regex test it returns an error

Usage:

const rules = require("schemerjs").rules;

let onlyWorld = rules.regex(/world/);

let pass = onlyWorld("world"); // returns undefined
let fail = onlyWorld("hello"); // returns an Error