JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 182910
  • Score
    100M100P100Q181841F
  • License BSD 2-Clause

A tiny runtime library for type assertions

Package Exports

  • @mapbox/fusspot

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@mapbox/fusspot) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@mapbox/fusspot

Build Status

Fusspot is a tiny runtime type-assertion library.

It can run in the browser as well as Node, and it's lightweight, flexible, and extensible.

Table Of Contents

Why does this exist?

Many existing runtime type-assertion libraries solve a special problem, like form validation or component props, or aren't great for usage in the browser, because of size or syntax. We wanted something similar to React's prop-types but not attached to a specific use case. So we ended up creating Fusspot.

Installation

npm install @mapbox/fusspot

Usage

Basic

In the example below we have a simple validation of an object and its properties. The outermost validator v.shape checks the shape of the object and then runs the inner validator v.arrayOf(v.string) to validate the value of names property.

@mapbox/fusspot exports a single object for its API. In the examples below we name it v (for "validator").

const v = require("@mapbox/fusspot");

assertObj = v.assert(
  v.shape({
    names: v.arrayOf(v.string)
  })
);

assertObj({ names: ["ram", "harry"] }); // pass
assertObj({ names: ["john", 987] }); // fail
assertObj({ names: "john" }); // fail

Required

By default null and undefined are acceptable values for all validators. To not allow null/undefined as acceptable values, you can pass your validator to v.required to create a new validator which rejects undefined/null.

// without v.required
assertObj = v.assert(
  v.shape({
    name: v.string
  })
);
assertObj({}); // pass
assertObj({ name: 'ram' }); // pass
assertObj({ name: undefined }); // pass
assertObj({ name: null }); // pass
assertObj({ name: 9 }); // fail

// with v.required
strictAssertObj = v.assert(
  v.shape({
    name: v.required(v.string)
  })
);

strictAssertObj({}); // fail
strictAssertObj({ name: 'ram' }); // pass
strictAssertObj({ name: undefined }); // fail
strictAssertObj({ name: null }); // fail
strictAssertObj({ name: 9 }); // fail

Composition

You can compose any of the Higher-Order Validators to make complex validators.

const personAssert = v.assert(
  v.shape({
    name: v.required(v.string),
    family: v.arrayOf(
      v.shape({
        name: v.required(v.string),
        relation: v.oneOf("wife", "husband", "son", "daughter")
      })
    )
  })
);

// assertion passes
personAssert({
  name: "john",
  family: [
    {
      name: "susy",
      relation: "wife"
    }
  ]
});

// assertion fails
personAssert({
  name: "harry",
  family: [
    {
      name: "john",
      relation: "father"
    }
  ]
});
// Throws an error
//   family.0.relation must be a "wife", "husband", "son" or "daughter".
const personAssert = v.assert(
  v.shape({
    prop: v.shape({
      person: v.shape({
        name: v.oneOfType(v.arrayOf(v.string), v.string)
      })
    })
  })
);

// assertion passes
personAssert({ prop: { person: { name: ["j", "d"] } } });
personAssert({ prop: { person: { name: ["jd"] } } });

// assertion fails
personAssert({ prop: { person: { name: 9 } } });
// Throws an error
//   prop.person.name must be an array or string.

Assertions

v.assert(rootValidator, options)

Returns a function which accepts an input value to be validated. This function throws an error if validation fails else returns void.

Parameters

  • rootValidator: The root validator to assert values with.
  • options: An options object.
  • options.apiName: String to prefix every error message with.
v.assert(v.equal(5))(5); // undefined
v.assert(v.equal(5), { apiName: "Validator" })(10); // Error: Validator: value must be a 5.

Primitive Validators

v.boolean

const assert = v.assert(v.boolean);
assert(false); // pass
assert("true"); // fail

v.number

const assert = v.assert(v.number);
assert(9); // pass
assert("str"); // fail

v.plainArray

const assert = v.assert(v.plainArray);
assert([]); // pass
assert({}); // fail

v.plainObject

const assert = v.assert(v.plainObject);
assert({}); // pass
assert(new Map()); // fail

v.string

const assert = v.assert(v.string);
assert("str"); // pass
assert(0x0); // fail

v.date

const assert = v.assert(v.date);
assert(98765); // pass
assert("1969-12-31T23:59:59.997Z"); // pass
assert(new Date()); // pass
assert(false); // fail
assert({}); // fail

v.coordinates

Passes when input is an [longitude, latitude], where longitude lies inclusively between [-180, 180] degrees and inclusively between [-90, 90] degrees.

const assert = v.assert(v.coordinates);
assert([150, 60]); // pass
assert([60, 150]); // fail

Higher-Order Validators

Higher Order Validators are functions that accept another validator or a value as their parameter and return a new validator.

v.shape(validatorObj)

Takes an object of validators and returns a validator that passes if the input shape matches.

const assert = v.assert(
  v.shape({
    name: v.required(v.string),
    contact: v.number
  })
);

// pass
assert({
  name: "john",
  contact: 8130325777
});

// fail
assert({
  name: "john",
  contact: "8130325777"
});

v.arrayOf(validator)

Takes a validator as an argument and returns a validator that passes if and only if every item of the input array passes the validator.

const assert = v.assert(v.arrayOf(v.number));
assert([90, 10]); // pass
assert([90, "10"]); // fail
assert(90); // fail

v.required(validator)

Returns a strict validator which rejects null/undefined along with the validator.

const assert = v.assert(v.arrayOf(v.required(v.number)));
assert([90, 10]); // pass
assert([90, 10, null]); // fail
assert([90, 10, undefined]); // fail

v.oneOfType(...validators)

Takes multiple validators and returns a validator that passes if one or more of them pass.

const assert = v.assert(v.oneOfType(v.string, v.number));
assert(90); // pass
assert("90"); // pass

v.equal(value)

Returns a validator that does a === comparison between value and input.

const assert = v.assert(v.equal(985));
assert(985); // pass
assert(986); // fail

v.oneOf(...values)

Returns a validator that passes if input matches (===) with any one of the values.

const assert = v.assert(v.oneOf(3.14, "3.1415", 3.1415));
assert(3.14); // pass
assert(986); // fail

v.range([valueA, valueB])

Returns a validator that passes if input inclusively lies between valueA & valueB.

const assert = v.assert(v.range([-10, 10]));
assert(4); // pass
assert(-100); // fail