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

@pinkyo/validatorjs

v0.0.9

Published

central validator to provide an alternative solution.

Downloads

5

Readme

validatorjs

Travis Coveralls npm (scoped) npm npm

validatorjs gives you nearly total control when you validate form values, compare to the existing validator tools. You can use it as a part to wrap the <input>, <textarea> and so on, with an error message display. although I try to emit convention usage, I can't get away from all and there are stil some convention usages.

Usage

$npm i @pinkyo/validatorjs -S

Document

this module includs several simple concepts.

  • field: field to validate, including id, name, groups, getter.

    • id: field id. required.
    • name: field name for error message display. optional. if not specified, id will be used.
    • groups: groups that field is in, it is used when check more than one field. optional. we can validate a group of fields a time, and all fields will be added to default group when registered.
    • getter: function to get field value, without no parameter. required.
  • validationChain: an array of functions to validate the field value. The following is a validation example:

        ({name, value}) => {
            if (value < 10) return;
            return `${name} must less than 10`;
        }
  • listener: function that will be invoked when validation is triggered.

  • resultCache: after validation, result will be saved to a cache for future's operataion, like access.

API

create a validator

A validator is the beginning of validatorjs usage, we operate on this object for the function we required.

function createValidator();

register a field to validator

function register(field, validationChain, callback);

validate groups, if not specified, default group. defalt group contains all fields.

function validate(groups, callback);

validate one field

function validateOne(id, callback) {

subscribe a listener

function subscribe(id, listener, callback);

clear all listen to a field

function clearListeners(id, callback);

update groups of a field

function updateGroups(id, groups, callback);

add a group to a field

function addGroup(id, group, callback);

remove a group to a field

function removeGroup(id, group, callback);

get one latest valiation result from result cache by id

function getOneResult(id);

get latest valiation results from result cache by groups

function getResults(groups);

clear result from result cache

function clearOneResult(id);

clear results from result cache by groups

function clearResults(groups);

deregister a field

function deregister(id, callback);

print validation info

function printValidationInfo();

print group info

function printGroupInfo();

print all info

function printAllInfo();

Example

The following is an example:

const id = 'test.id';
const group = 'test.group';
const invalidGroup = "test.group.invalid";
const groups = [group];
const name = 'test.name';
const getter =() => 5;
const tip = 'value must less than 5.';
const validationChain = [({name, value}) => value < 5? '': tip];
const field = {id, groups, getter, name};

const validator = createValidator();
validator.register(field, validationChain);
validator.addGroup(id, invalidGroup);
const result = validator.validate([invalidGroup]);

Tools

we provide a tools diretory that contains some commomly used function to reduce workload. and it's alternative. Tools

Contribution

report issue and pull request are welcomed.

LICENSE

MIT