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 🙏

© 2025 – Pkg Stats / Ryan Hefner

coc-validator

v4.0.4

Published

coc engine based validator

Readme

COC.JS Validator

What is COC.JS Validator

Javascript Validator based on COC core engine.

  • Built with 29 rules ready.
  • Able to work with your own custom validator.
  • Easy and powerful Status Delivery

Install with npm

npm install coc-validator

Get Started

After installing from npm, all you need to do then is importing validator class and you are ready!

import coc from 'coc-validator'

Validator

Coc Validator is a validation class that was made to validate user input with a ready made validators that will cover most of common cases, you can also add a custom rules and custom error delivery messages.

Rules

[
   "HasValue",                  //Arguments: none
   "SameAs",                    //Arguments: none
   "IsString",                  //Arguments: none
   "IsEmail",                   //Arguments: none
   "IsNumeric",                 //Arguments: none
   "IsNumericString",           //Arguments: none
   "IsDateString",              //Arguments: none
   "IsArray",                   //Arguments: none
   "IsObject",                  //Arguments: none
   "IsEvenNumber",              //Arguments: none
   "IsOddNumber",               //Arguments: none
   "NumberGreaterThan",         //Arguments: Number min
   "NumberLessThan",            //Arguments: Number max
   "NumberBetween",             //Arguments: Object { min: Number, Max: Number }
   "MaxDate",                   //Arguments: MomentInstance min
   "MinDate",                   //Arguments: MomentInstance max
   "DateBetween",               //Arguments: Object { MomentInstance min, MomentInstance Max }
   "MatchesRegex",              //Arguments: Regex regex
   "MinLength",                 //Arguments: Number min
   "MaxLength",                 //Arguments: Number max
   "LengthBetween",             //Arguments: Object { min: Number, Max: Number }
   "MinArrayLength",            //Arguments: Number min
   "MaxArrayLength",            //Arguments: Number max
   "Each",                      //Arguments: Rules will be applied on each element in the array
   "Keys",                      //Arguments: Object includes the desired keys to be validated, each of them has set of rules
   "Remote",                    //Arguments: Object args than includes:
                                  //Object options (axios args eg: { url: '/foo', method: 'get'})
                                  //Function callback to be excuted on resolvation, consider res as a parameter
                                  //AxiosInstance agent --optional
                                  // Function catch to be excuted on fail --optional
   "PreConditions",           //Arguments: Array validators
                                  //Each of them should return true , or error messagee or false
]

Rules, Args and Options

// Standard
const standardOptions = {
  HasValue: {
    args: true,
    active: true,
    message: 'This field is required',
    icon: 'fa fa-error',
  }
}

// Only the needed attributes
const onlyIfNeeded = {
  HasValue: { icon: 'fa fa-error'},
  MaxLength: {
      args: 3,
      message: 'Whoops!, length cant pass |*args*|'
    }
}

// Quick
const quickOptions = { HasValue: true, MaxLength: 3 }

Examples

import Validator from 'coc-validator'

const person = { name: 'Jhon Doe', age: 24, sports: [ 'basket', 'ping-pong' ] }

const rules = {
   HasValue: true,
   IsObject: true,
   Keys: {
     name: { HasValue : true },
     age: { IsNumeric: true, NumberLessThan: 20 }
   }
}

const v = new Validator( person, rules )
const result = await v.Run()

The expected result is

{
  attemp:  0
  attemps:  0
  code:  12
  error:  "NumberLessThan"
  icon:  "ivu-icon ivu-icon-ios-alert-outline"
  instance:  "coc-validator"
  message:  "This number must be less than 20"
  path:  (2) ["root",  "age"]
  val:  24
  valid:  false
 }

The same demo with custom error messages

import Validator from 'coc-validator'

const person = { name: 'Jhon Doe', age: 24, sports: [ 'basket', 'ping-pong' ] }

const rules = {
   HasValue: true,
   IsObject: true,
   Keys: {
     name: { HasValue : true },
     age: { 
            IsNumeric: true,
            NumberLessThan: {
              args: 20,
              message: 'Age cant be greater than 20'
            }
       }
   }
}

const v = new Validator( person, rules )
const result = await v.Run()

Or you can event set a dynamic error message by passing the message like this

{args: 20, message: 'Age cant be greater than |*args*|'}

in this way, Coc will replace |*args*| by the value in your args.

Another case is when our args is an object, such as NumberBetween rule, it expects args to be { max: 20, min: 10 } for example, in this case dynamic error message will be something like this.

{ 
  args: { max: 20, min: 10 },
  message: 'Age cant be greater than |*args.max*| and less than |*args.min*|'
}

Another case also is validating using user custom validators, which could happen in PreConditions Validator for example. The args right there is an array of functions, so what if i want a special message for each of them. moreover, what if i want each of them to append an error message to some fixed prefix.

The answer is, simply let the validators returns true or error message

eg

const custom = val => val > 20 : 'can not be less than 20'

const rules = {
  Keys: {
    age: {
      PreConditions: {
        args: [ custom ],
        message: 'age |*args*|'
      }
    }
  }
}

so if custom returns the string "can not be less th.." then coc will replace |*args*| with this and the result will be age can not be less than 20