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

@wonderlandlabs/schema

v1.0.0

Published

An object validation system

Downloads

3

Readme

Schema

Schema is a detailed object validation system for objects. Unlike Yup which defines defines validation tests as methods Schema takes straight functions or is.js function name strings and can return multiple errors (if configured to).

Assumptions

  • The object under test is an object whose properties can be accessed and tested value by value. It can be a POJO, instance, or anything else with readable props. If you want to test single values look is.js.
  • Some of the objects' properties might be absent.
  • The object is (relatively) flat. Deep schema with nested validation requires a little work by the user.

Features

  • In some use cases you may only want to validate a subset of fields; i.e., a record without an ID might be valid as a pre-sent item, but not when fetched from the server; or you may be working on a multi-part form.
  • Validation works in one of two modes:
    1. stopIfInvalid: when a field has an error, no further tests are executed.
    2. (not stopIfInvalid): even if one error is found, fetch the others for a full diagnostic.
  • The second mode is more difficult to code as your tests cannot assume that the value has passed previous filters.
  • Schemas can generate instances pre-seeded with defaults, and optionally blended with a set of values via the instance() method of schema.

API

Schema

Constructor:(name, fieldDefs)

  • name: string
  • fieldDefs: object|array either a key/value list of field defs or an array of field def objects

const sch = new Schema('user', {
    name: 'string',
    age: 'integer',
    created: {
      required: false,
      type: 'date',
      validator: (value) => value.getTime() < Date.now(),
    },
});

validate(record)

returns a RecordState object; useful properties are:

  • errors{Array} errors from all fields
  • fields{Map} errors by field
  • isValid{Boolean}

instance(values?)

returns a new instance with defaults provided by the field schema

FieldDef

constructor(name: string, params: object, schema: Schema)

Parameters (all optional) include:

  • type{String} any value present in is.js
  • validator{var} a validator, or an array of validators (see below)
  • required{boolean} (default: false) if false, then 'falsy' values will not generate errors. Note -- 'falsy' values might violate type so use with care.
  • stopIfInvalid{boolean} (default: false) if a single validator in a series fails (including the type validator) do not perform subsequent tests.
  • defaultValue{var} a value to be used (by Schema.instance method) if a record doesn't have a value. Useful for forms. note - can be a function - which will be called unless the type of the field IS a function for each instance call; this ensures objects, arrays, etc., can be made unique.
  • invalidMessage{String} (default: "[name] required") Any validation test that returns true (not just truthy) will display this message.
  • requiredMessage{String} (default: "[name] required") Displayed when a required field tests an absent value.

validators

Validators are either:

  1. a string (name of an is.js method)
  2. a function
  3. an array of the above in any combination

Validators return either false (when value is good) or a string (when value is bad).

Note that type is a validator -- however it is always a string, and it is evaluated before the validator tests.