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

modelism

v1.2.3

Published

A DSL for defining self-validating models

Downloads

10

Readme

modelism

A schema language for validating JavaScript objects

Example

Defining validation models

Use compact schema definitions to define model constructors:

var model = require('modelism');

var Company = model({
  name: 'Company',
  properties: {
    name: {
      presence: {
        message: 'Name is required'
      },
      format: {
        pattern: /^[A-Za-z0-9\s]*$/,
        message: 'Letters, numbers and whitespace only'
      }
    },
    registered: {
      type: 'boolean'
    },
    registrationNumber: {
      type: 'number',
      presence: {
        message: 'Registered companies must have registration numbers',
        enabled: function(company) {
          return company.registered;
        }
      }
    }
  }
});

Creating models

Models can be instantiated with property values:

var leftorium = new Company({
  name: 'The Leftorium',
  email: '[email protected]'
});

Validating models

Models have schemas, so they can validate themselves and group the resulting validation errors by property:

leftorium.schema                        // -> { name: 'Company', properties: [...] }

leftorium.isValid()                     // -> true
leftorium.name = '';
leftorium.isValid()                     // -> false

var validation = leftorium.validate();  // -> { errors: [...] }
validation.errors;                      // -> [{ property: ..., message: ... }, ...]
validation.errorsOn('name')             // -> ['is required']

Relationships between models

Schema properties can refer to other schemas by name. Use a factory to create objects with different schemas and relationships between them:


var Contact = model({
  name: 'Contact',
  properties: {
    firstName: { type: 'string'},
    company: { schema: 'Company' }
  }
});

var data = {
  firstName: 'Homer',
  company: {
    name: 'Nuclear power plant'
  }
}
var factory = model.factory(Company, Contact);
var contact = factory.create('Contact', data);
contact.company.validate() // -> { errors: [...] }

Validating related models

Validating a model will validate any related models:

var ned = factory.create('Contact', {
  company: { name: '' }
})
ned.validate().errorsOn('company.name') // -> ['is required']

Property schema language

The property schema language provides a compact syntax for assigning validations to model properties. For example, here's how you could add the built in presence validator to your model's brand property:

var Television = model({
  properties: {
    brand: {
      presence: true
    }
  }
});

Just like presence, you can register your own domain-specific validators and extend the property validator language:

model.validators.onlySweet = function(property, enabled) {
  if (enabled) {
    property.addValidator({
      validate: function(taste) {
        return taste == 'sweet' ? [] : [{ message: 'must be sweet' }];
      }
    });
  }
}
var Cake = model({
  name: 'Cake',
  properties: {
    taste: { onlySweet: true },
    aftertaste: { onlySweet: false }
  }
});
new Cake({ taste: 'sweet', aftertaste: 'sweet' }).isValid()   // -> true
new Cake({ taste: 'savory', aftertaste: 'sweet' }).isValid()  // -> false
new Cake({ taste: 'sweet', aftertaste: 'savory' }).isValid()  // -> true

License

MIT