@hornbeck/validators

1.0.1 • Public • Published

@hornbeck/validators

npm npm npm npm bundle size npm bundle size

Validator functions for usage in form.

npm install --save @hornbeck/validators

Documentation

All the functions are automatically curried and should be easy to use for any usecase.

This is to allow for variable amount of prefilling of values. All of the functions can be called in any of the follow ways.

import { max } from "@hornbeck/validators";

const max12 = max(12);

const max12WithMessage = max12("Message");

const max13WithMessage = max(13, "Message");

// Return "Message" if `a` is smaller than 12, otherwise undefined.

max12WithMessage(a);
max12("Message", a);
max(12, "Message", a);
max(12)("Message")(a);

schema

Using schema you'll be able to define the rules for a validation function by passing in an object in the same shape as your values.

It automatically wraps the entire validation result if any of your validations are run asynchronously.

import { schema, required, email } from "@hornbeck/validators";

const validate = schema({
    firstName: required("Required"),
    email: [required("Required"), email("Must be a valid email")]
    address: {
        line1: required("Required"),
        postcode: required("Required"),
    }
})

validate({
    firstName: "",
    email: "asdf@",
    address: {
        line1: "",
        postcode: "",
    },
})
// returns
{
    firstName: "Required",
    email: "Must be a valid email",
    address: {
        line1: "Required",
        postcode: "Required",
    },
}

Validators

  • required

  • maxLength

  • minLength

  • max

  • min

  • pattern

  • positive

  • negative

  • creditCard - validate the format of credit card number. (disclaimer: this only verifies that a credit card number matches the format, it does not validate or verify the creditcard is actually usable)

  • guid

  • email

  • validateFn - a helper for creating your own validation functions

Note: the following two validators are made specifically for @hornbeck/react-form, but a similar thing can probably be done for any other form handling package you use.

  • smaller
  • greater

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.180latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.180
1.0.01
0.2.20
0.2.10
0.2.00
0.1.00

Package Sidebar

Install

npm i @hornbeck/validators

Weekly Downloads

23

Version

1.0.1

License

MIT

Unpacked Size

12 kB

Total Files

8

Last publish

Collaborators

  • madshornbeck