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

@kilix/functional-validation

v2.5.1

Published

Lightweight and customisable validation, built through composition

Downloads

2

Readme

functional-validation

build status codecov coverage npm

Lightweight and customisable validation, built through composition.

Why

Validation is complicated in JavaScript. The most common solution is to use a validator object-shaped, à la joy or yup. However, I find it mostly made to make it straightforward to write simple validation, more complicated ones need to use specific APIs. Since validation can also be seen as a list of validations model -> ?error, you don't need a highly abstract API to be able to run any kind of validation. Moreover, with little overhead, by composing functions, you can compose validations: a validator that validates an order can use another one that validates the validity of a product!

How to use

There are two core functions, validateModel: Array<Validation> => Model => Array<error>, where Validation: Model => Array<error> | ?error and createValidation : params => Model => ?error. I'll come back later on the createValidation params, but you can observe that both the functions returned by validateModel and createValidation can used to create validations used by validateModel. Moreover, createValidation doesn't return anything really fancy, it just takes a given model, and can return an error. So you're always free to skip it if one of your validations is too complicated to work with out API (or if it makes it clearer to work without). This lets you use the same pattern (model => ?error, or model => Array<error>) for any kind of validation.

Here is a more concrete example:

const validatorNewUser = validateModel([
  // This returns an error {field: 'age', error: 'underaged'} if model.age < 18
  createValidation(
    'age',  // used to be able to set the field value of the error
    model => model.age, // returns the part of the model that will be checked
    // checks the relevant part of the model
    // returns the name of the error as a string if there is one
    age => age >= 18 ? null : 'underaged',
  ),
]);

You can argue that using createValidation is pretty verbose (and you'd be right), however, you need to keep in mind that this library aiming for a more FPesque code style, model => model.age might already be declared somewhere else, and that checking if an age is underaged can be useful to validate other models. Moreover, we do provide you with several generic value validators.

Lastly, createValidation is the low-level validator, we have some higher-order ones, to avoid too-verbose validations:

const validateAge = age => age >= 18 ? null : 'underaged';
// This is equivalent to the previous one, since it's that common to test a field without needing
// to use another key as the error name
createSimpleValidation(validateAge,'age');

createNestedValidation(validateAge,['person','age']);
// is equivalent to
createValidation('person.age',  model => model.person.age, validateAge);

runConditionValidation(
  model => model.age < 18,
  [createSimpleValidation(authorised => authorised ? null : 'not','authorisedByParents')]
);
// is equivalent to
model => model.age < 18
  ? model.authorisedByParents ? null : 'not'
  : null;