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

tcomb-validator-plus

v1.3.9

Published

tcomb-validator-plus ====================

Downloads

33

Readme

tcomb-validator-plus

Advanced validation helpers for tcomb-validation

Features

  • Used validator.js for strings
  • Custom error messages
  • Context binding
  • Compile from simple schema

Extending rules

import createValidator from 'tcomb-validator-plus'
import extend from 'tcomb-validator-plus/extend'
import gt from 'tcomb-validator-plus/getType'
import {intersection} from 'tcomb'

function fakeLocalize(message, context) {
    return {
        message,
        context
    }
}

const validate = createValidator(fakeLocalize)

const password = (min, max) => intersection([
    gt('isLengthMin')(min),
    gt('isLengthMax', 'password greate than #{val}')(max)
])

extend('password', password)


const schema = struct({
    password: gt('password')(3, 5),
})
const compiledSchema = compile({
    password: 'password'
})

const data = {
    password: '12'
}

const errors = {
    errors: {
        password: {
            context: {
                actual: '12',
                val: 3
            },
            message: 'isLengthMin'
        }
    },
    isError: true,
    params: {
        password: '12'
    }
}

const obj = validate(schema)(data)
assert.deepEqual(obj, errors)
const obj2 = validate(compiledSchema)(data)
assert.deepEqual(obj2, errors)

Simple usage

// simple-1.js
import assert from 'power-assert'
import createValidator from 'tcomb-validator-plus'
import compile from 'tcomb-validator-plus/compile'

function fakeLocalize(message, context) {
    return {
        message,
        context
    }
}

const validate = createValidator(fakeLocalize)

const myValidate = validate(compile({
    confirmed: {v: 'required', l: 'check confirm'},
    password: [
        ['isLengthMin', 3],
        {l: 'password greate than #{val}', v: ['isLengthMax', 5]}
    ],
    confirmPassword: [
        ['isLengthMin', 3],
        {l: 'password greate than #{val}', v: ['isLengthMax', 5]}
    ],
    user: {v: 'required', l: 'user required'},
    opts: {v: ['listOf', ['opt1', 'opt2']], l: '#{actual} is not a one of #{val}'},
    __: {v: ['equal', 'password', 'confirmPassword'], l: 'Passwords are not equal'}
}))

const data = {
    confirmed: false,
    password: '1234',
    confirmPassword: '1235',
    user: '',
    opts: ['opt1', 'opt3']
}

const errors = {
    errors: {
        confirmed: {
            message: 'check confirm',
            context: {
                actual: false
            }
        },
        user: {
            message: 'user required',
            context: {
                actual: ''
                path: ['user'],
            }
        },
        opts: {
            message: '#{actual} is not a one of #{val}',
            context: {
                val: 'opt1, opt2',
                actual: 'opt3'
            }
        }
    },
    isError: true,
    params: {
        confirmed: false,
        password: '1234',
        confirmPassword: '1235',
        user: '',
        opts: ['opt1', 'opt3']
    }
}

const obj1 = myValidate(data)
assert.deepEqual(obj1, errors)

const data2 = {
    confirmed: true,
    password: '1234',
    confirmPassword: '1235',
    user: 'test',
    opts: ['opt1', 'opt2']
}

const errors = {
    errors: {
        __: {
            context: {
                from: 'password',
                to: 'confirmPassword',
                actual: {
                    confirmed: true,
                    password: '1234',
                    confirmPassword: '1235',
                    user: 'test',
                    opts: [
                        'opt1',
                        'opt2'
                    ]
                }
            },
            message: 'Passwords are not equal'
        }
    },
    isError: true,
    {
        confirmed: true,
        password: '1234',
        confirmPassword: '1235',
        user: 'test',
        opts: ['opt1', 'opt2']
    }
}

const obj2 = myValidate(data2)
assert.deepEqual(obj2, errors)

Promise usage

import {validate} from 'tcomb-validation'
import assert from 'power-assert'
import createValidator from 'tcomb-validator-plus'
import promisifyValidate, {ParamsError} from 'tcomb-validator-plus/promisifyValidate'
import compile from 'tcomb-validator-plus/compile'

function fakeLocalize(message, context) {
    return {
        message,
        context
    }
}

const validate = promisifyValidate(createValidator(fakeLocalize))

const myValidate = validate(compile({
    confirmed: {v: 'required', l: 'check confirm'},
    password: [
        ['isLengthMin', 3],
        {l: 'password greate than #{val}', v: ['isLengthMax', 5]}
    ],
    confirmPassword: [
        ['isLengthMin', 3],
        {l: 'password greate than #{val}', v: ['isLengthMax', 5]}
    ],
    user: {v: 'required', l: 'user required'},
    opts: {v: ['listOf', ['opt1', 'opt2']], l: '#{actual} is not a one of #{val}'},
    __: {v: ['equal', 'password', 'confirmPassword'], l: 'Passwords are not equal'}
}))

const data = {
    confirmed: false,
    password: '1234',
    confirmPassword: '1235',
    user: '',
    opts: ['opt1', 'opt3']
}

const errors = {
    errors: {
        confirmed: {
            message: 'check confirm',
            context: {
                actual: false
            }
        },
        user: {
            message: 'user required',
            context: {
                actual: ''
                path: ['user'],
            }
        },
        opts: {
            message: '#{actual} is not a one of #{val}',
            context: {
                val: 'opt1, opt2',
                actual: 'opt3'
            }
        }
    },
    isError: true,
    params: {
        confirmed: false,
        password: '1234',
        confirmPassword: '1235',
        user: '',
        opts: ['opt1', 'opt3']
    }
}

myValidate(data)
    .then(params => {
        assert.deepEqual(params, data)
    })
    .catch(e => {
        assert.deepEqual(e.errors, errors)
    })