Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

johnste/fastidious

Repository files navigation

fastidious

Very small Javascript library to validate objects, similar to prop-types.

usage

import { getErrors, validate } from "./fastidious.js";

getErrors({ value: true }, { value: validate.boolean }); // []

const schema = {
  match: validate.oneOf([validate.string, validate.function(), validate.regex]).isRequired
};

getErrors({}, schema); // [ 'Error: Expected "config.match" to be oneOf: [string,function,regex]' ]

getErrors({ match: "test" }, schema); // []

Validator API

validate.boolean
validate.string
validate.number
validate.function([argNames])
validate.regex
validate.value(expectedValue)
validate.shape(schema)
validate.arrayOf(validator)
validate.oneOf([validators]))

Advanced schema example

Used in Finicky to validate config file

const schema = {
  defaultBrowser: validate.string.isRequired,
  options: validate.shape({
    hideIcon: validate.boolean,
    urlShorteners: validate.arrayOf(validate.string)
  }),
  rewrite: validate.arrayOf(
    validate.shape({
      match: validate.oneOf([validate.string, validate.function("options"), validate.regex])
        .isRequired,
      url: validate.oneOf([validate.string, validate.function("options")]).isRequired
    }).isRequired
  ),
  handlers: validate.arrayOf(
    validate.shape({
      match: validate.oneOf([
        validate.string,
        validate.function("options"),
        validate.regex,
        validate.arrayOf(
          validate.oneOf([validate.string, validate.function("options"), validate.regex])
        )
      ]).isRequired,
      browser: validate.oneOf([
        validate.string,
        validate.shape({
          name: validate.string.isRequired,
          type: validate.oneOf(["name", "bundleIdentifier"]).isRequired
        })
      ]).isRequired
    })
  )
};

Credits

Icon made by Smashicons from www.flaticon.com is licensed by CC 3.0 BY

About

Very small Javascript library to validate object shapes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published