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

easen-models

v0.4.0

Published

ES6 models & validation

Downloads

586

Readme

Easen Models

Travis Code Climate Test Coverage NPM Downloads

How to use

Create your first model

Firstly, you have to create your own model:

const { createModel, types: t } = require('easen-models')

const Post = createModel({
  id: t.int,
  title: t.string,
  published: t.bool,
  createdAt: t.date,
  updatedAt: t.date
})

Use model

Secondly, just create instance of this model:

const post = new Post({
  id: '10',
  title: 'Post title',
  published: 1,
  createdAt: '2017-01-01T15:00:00',
  updatedAt: Date.now()
})

Handle validation errors

In case of current types, values will be transformed to correct type (e.g. id to int). When something will be wrong, ModelValidationError will be thrown, so you can handle errors:

const { ModelValidationError } = require('easen-models')

try {
  const post = new Post({
    id: '10',
    title: 'Post title',
    published: 1,
    createdAt: '2017-01-01T15:00:00',
    updatedAt: Date.now()
  })
} catch (e) {
  if (e.name === 'ModelValidationError') {
    // Some values are wrong, you can look at additional errors at e.list
    // Or you can dump whole error with e.toJSON() method
  } else {
    // Rethrow errors not connected to validation
    throw e
  }
}

Prepare your own validators

Behind the scenes, models are trying to instantiate values and checks for errors. Let see some examples of preparing your own validators:

const { createModel, types: t } = require('easen-models')

const Post = createModel({
  id: t.int.assert(v => v < 10, 'ID must be smaller than 10'),
  title: t.string.pass(v => v.toUpperCase()),
  published: t.assert(v => v === true, 'Post must be published'),
  likesPercentage: t.pass(parseFloat).pass(v => v * 100),
  slug: v => ('' + v).toLowerCase().replace(/[^a-z]/g, '-')
})

Validators/types are just simple functions, which can throw error. It means, that you can use any other libraries to use there. When Model will detect error thrown, it will throw ModelValidationError.

Alternatively, you can use simple syntax with pass & assert helpers. Every validation included in easen-models allow chaining by default. pass is just passing a value to next function, assert will make assertion (check if passed function results with truthy value) and throw error if not. You can use these methods as many times in chain you want.

Updating object

You have to simply update a value - it will be automatically transformed. Everything is using getters & setters:

const { createModel, types: t } = require('easen-models')

const Post = createModel({
  id: t.int,
  title: t.string,
  published: t.bool,
  createdAt: t.date,
  updatedAt: t.date
})

const post = new Post({
  id: '10',
  title: 'Post title',
  published: 1,
  createdAt: '2017-01-01T15:00:00',
  updatedAt: Date.now()
})

console.log(post.createdAt) // instance of Date, 2017-01-01T15:00:00.000Z

post.createdAt = '2015-01-01'
console.log(post.createdAt) // instance of Date, 2015-01-01T00:00:00.000Z

Getting raw object

You can get copy of raw object using built-in function:

const { raw } = require('easen-models')
const value = raw(post)

If you want to serialize object, you can simply use just JSON.stringify

Development

Mocha with Expect.js are used for tests, with Wallaby as additional runner for development. Code style standard is StandardJS.

To do

  • Write unit tests
  • Extend description, improve documentation
  • Write oneOfType validator
  • Prepare table with available validators in README file