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

@prostojs/valido

v0.0.2

Published

Instance Factory and Instance Registry for Metadata based Dependency Injection scenarios

Downloads

7

Readme

prostojs/valido

Valido is a validation framework. It implements the validation routines against objects and single values with no coupling to any of the validation definition solution. So the definition of validation is passed via options callbacks to the instance of Valido.

Valido supports:

  • validation of a single value (param)
  • validation of an object (class)
  • validation of a nested object (class)
  • validation of an array items (as a single values or as nested objects)

Valido provides a validation result in a format that matches the validated object, the values of validated properties are filled with the error texts. If the validation passed then the returned value is true.

Install

npm install @prostojs/valido

How to use it

const { Valido } = require('@prostojs/valido')

const valido = new Valido({
    // errorLimit specifies the amount of errors after
    // which it'll stop validation of the object
    // (10 by default)
    errorLimit: 10,

    // getDtoMeta is a callback used to provide the
    // Data Transfer Object (DTO) definition (metadata)
    // Here you can inject whatever validation definition
    // you want to use (e.g. read class metadata, read DB etc.)
    //
    // @param value - the object itself
    // @param type - the object type (class)
    async getDtoMeta(value, type?) {
        return {
            // label is optional, can be used in error texts
            label: 'value label',
            
            // dto optional, defines dto options
            dto: { 
                // errorLimit defines maximum errors
                // to stop validation (by default 10)
                errorLimit: 10,

                // allowExtraFields, when true, will
                // ignore any extra fields in object
                allowExtraFields: false,
            },

            // typeName is optional, defines the type
            // name, usually it is classname (type.name)
            // may be used in error texts
            typeName: 'MyClassToValidate',

            // requiredProps is an array of string | symbol
            // that lists all the required props
            requiredProps: ['prop1', 'prop2'],           
        } // TValidoDtoMeta
    },

    // getDtoParamMeta is a callback used to provide
    // param validation definition (metadata)
    // Here you can inject whatever validation definition
    // you want to use (e.g. read method params metadata, read DB etc.)
    //
    // @param value - the param value
    // @param type - the param type
    // @param key - the param key
    async getDtoParamMeta(value, type, key) {
        return {
            // required is optional, specifies
            // if the param (field) is required
            required: true,

            // label is optional, can be used in error texts
            // to label the param (field)
            label: 'Field label',

            // validators is optional, contains an array
            // of validator functions
            validators: [
                ({value, label, key}) => 
                    value > 0 ? true : `${label || key} must be gt than 0`,
            ],

            // validatorsOfItem is optional, contains
            // validator functions for each item (for arrays)
            validatorsOfItem: [
                ({value, label, key}) => 
                    value > 1 ? true : `${label || key} must be gt than 1`,
            ],

            // arrayType is optional, marks the param (field)
            // as an array to apply array checks
            // can take boolean value
            // or an object with options:
            arrayType: {
                // itemType is optional, is a callback that returns
                // the item type (class constructor)
                itemType: () => MyClass,

                // minLength is optional, specifies the
                // min expected length of the array
                minLength: 1,

                // maxLength is optional, specifies the
                // max expected length of the array
                maxLength: 5,
            },

            // type is optional, provides the type of the
            // current param (field)
            type: type,
        } // TValidoParamMeta
    },
})

// when all the injections with proper validation
// definitions are set we are ready for validations:

// validation of an object:
// assuming that we have some class "MyClass"
// that supports validations
// and some object "myObject" that should
// match to the MyClass validation definition
valido.validateDTO(myObject, MyClass)

// validation of a single value
valido.validateParam(myObject, {
    ... // TValidoParamMeta
})

Pre-defined validation functions

Valido provides few generic validators:

  • validoIsTypeOf('type_name') - validates if the value is of specified type
  • validoIsString({...}) - validates if the value is of string type, accepts options like minLength, maxLength, regex and errorText
  • validoIsNumber({...}) - validates if the value is of number type, accepts options like min, max and errorText
  • validoIsBoolean() - validates if the value is of boolean type

Example of usage

{
    ...
    validators: [
        // will validate the string of min length of 5 characters
        validoIsString({ minLength: 5 }),

        // will validate the number which must be less than 101
        validoIsNumber({ max: 100 }),

        // will validate that the value is of boolean type
        validoIsBoolean(),

        // will validate that the value is of object type
        validoIsTypeOf('object'),
    ],
    ...
}

How to add a custom validator

Any function that satisfies TValidoFn type can be a custom validator function, but usually it makes sense to write a factory function that accepts some options and returns TValidoFn:

// an example of validator that
// checks if the value is compatible with Date
function isDateCompatible(opts?: {
    errorText?: string
}): TValidoFn {
    return ({value, label, key}) => {
        const notCompatible = Number.isNaN(new Date(value).getTime())
        return notCompatible ? errorText || `"${ label || key || '' }" not compatible with Date` : true
    }
}