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

simple-object-schema

v0.1.5

Published

Simple schema for JavaScript objects, with validation and flexible error messages.

Downloads

5

Readme

simple-object-schema

Simple schema for JavaScript objects, with validation and flexible error messages.

Usage

$ npm install simple-object-schema --save
const Schema = require('simple-object-schema').default;
const messageTypes = require('simple-object-schema').messageTypes;

or with import

import Schema, { messageTypes } from 'simple-object-schema'

Examples

ES2015

import Schema, { messageTypes } from 'simple-object-schema';

class FooSchema extends Schema {
  constructor(options) {
    super(options);
    this.schema = {
      id: this.define.type(Number).isRequired(),
      name: this.define.type(String).isRequired().label('Display Name'),
      createdAt: this.define.type(Date).defaultsTo(Date.now()).label('Created At'),
    }
  }

  validate(values) {
    return super.validate(values, this.schema);
  }
}

const foo = new FooSchema();

const results = foo.validate({
  id: 100,
  name: 'nanopx',
});

console.log(results);
// { values: { id: 100, name: 'nanopx', createdAt: 1465487357721 },
//   errors: null }

Using require()

const Schema = require('simple-object-schema').default;
const messageTypes = require('simple-object-schema').messageTypes;

const schema = new Schema();

const fooSchema = {
  foo: schema.define.type(Number).min(100).max(200).equalsTo(150).isRequired().label('FOO'),
  bar: schema.define.type([Number, Boolean]).defaultsTo(100).isRequired().label('BAR'),
  baz: schema.define.type(String).match(/hello/).isRequired(),
  abc: schema.define.type(String).in(['ABC', 'DEF']),
};

// Pass
console.log(schema.validate({ foo: 150, bar: undefined, baz: 'hello' }, fooSchema));
// { values: { foo: 150, bar: 100, baz: 'hello' },
//   errors: null }


// Fail
console.log(schema.validate({ foo: 10000, bar: 'string', baz: 'helo', notDefinedInSchema: 'hello' }, fooSchema));
// { values: null,
//   errors:
//    { __root__: [ 'Key "notDefinedInSchema" is not defined in schema' ],
//      foo:
//       [ '"FOO" must be less than 200',
//         '"FOO" must be equal to "150"' ],
//      bar: [ '"BAR" must be one of "Number, Boolean", but the type of given value was "String"' ],
//      baz: [ '"baz" does not match pattern "/hello/"' ] } }


// Fail, with partially overridden message
const locale = {};
locale[messageTypes.IS_REQUIRED] = '{{name}} IS REQUIRED!';
const schema2 = new Schema({ locale: locale });
console.log(schema2.validate({ foo: null, bar: 'string', baz: 'helo', notDefinedInSchema: 'hello' }, fooSchema));
// { values: null,
//   errors:
//    { __root__: [ 'Key "notDefinedInSchema" is not defined in schema' ],
//      foo:
//       [ 'FOO IS REQUIRED!',
//         '"FOO" must be a "Number"',
//         '"FOO" must be equal to "150"' ],
//      bar: [ '"BAR" must be one of "Number, Boolean", but the type of given value was "String"' ],
//      baz: [ '"baz" does not match pattern "/hello/"' ] } }


// Using locale preset
const schema3 = new Schema({ locale: Schema.locales.ja });
console.log(schema3.validate({ foo: null, bar: 'string', baz: 'helo', notDefinedInSchema: 'hello', abc: 'FOO' }, fooSchema));
// { values: null,
//   errors:
//    { __root__: [ 'スキーマに"notDefinedInSchema"が定義されていません。' ],
//      foo:
//       [ '"FOO"は必須です。',
//         '"FOO"は数字でなければなりません。',
//         '"FOO"は"150"と同じでなければなりません。' ],
//      bar: [ '"BAR"は"数字, 真偽"の何れかの型でなければなりません。指定された値は"文字列"型でした。' ],
//      baz: [ '"baz"は正規表現の"/hello/"に一致しませんでした。' ],
//      abc: [ '"abc"は"ABC, DEF"の何れかと一致しなければなりません。' ] } }

Todo

  • [ ] Add more docs

License

MIT