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

@nvidia1997/js-validator

v1.2.9

Published

This is simple validation library written in typescript and compiled down to javascript.

Downloads

51

Readme

@nvidia1997/js-validator

This is simple validation library written in typescript and compiled down to javascript.

If you need this for react app, please use this wrapper react-js-validator.

How to use:

load the library:

For Node js
const { Validator } = require("@nvidia1997/js-validator");
For environments that support "import" keyword
import { Validator } from "@nvidia1997/js-validator";
Create new validator instance
const mainValidator = new Validator();

// or you can pass a config
const mainValidatorExtended = new Validator({
  onStateChanged: (validatorId) => console.log("this validator was updated"),
  overrideOnIdConflict: true, // whether to override if there"s sub validator defined with this name.It throws if false
  throwIfNotValidated: false// whether to  throw exception if you try to access hasErrors or some other post validation related prop
});
Define some sub-validators:

Important note !

Using allowNull will skip execution of other sub-validations if passed passedValue === null

Using allowUndefined will skip execution of other sub-validations if passed passedValue === undefined

Using notRequired will skip execution of other sub-validations if passedValue === null || passedValue === undefined

Most of the validation methods have extra param "flipLogic" (NOT required) which reverses the validation logic. Example: if we want to look for emails we use "email("Error message")", if we want to look for everything but emails, we use "email("Error message", true)"

const flipLogic = false;

number

 simpleValidator
  .number(12, "numberValidator", "Error: value is not a number")
  .allowNull() // null values won't trigger errors
  .allowUndefined() // undefined values won't trigger errors
  .notRequired() // allowNull and allowUndefined combined
  .greaterThan(3, "Error: value is not >3", flipLogic)
  .greaterThanOrEqual(5, "Error: value is not >=5", flipLogic)
  .lessThan(2, "Error: value is not <2", flipLogic)
  .lessThanOrEqual(2, "Error: value is not <=2", flipLogic)
  .exclusiveBetween(1, 4, "Error: value is not 1>value<4", flipLogic)
  .inclusiveBetween(1, 2, "Error: value is not 1>=value<=2", flipLogic)
  .notZero("Error: value must be !=0", flipLogic)
  .notInfinity("Error: value is not ", flipLogic)
  .odd("Error: value % 2 == 0", flipLogic)
  .even("Error: value % 2 != 0", flipLogic)
  .integer("Error: value is not integer", flipLogic)
  .canBeDividedBy(3, "Error: value % 3 != 0", flipLogic)
  .equalTo(6, "Error: value !== comparissonValue", flipLogic)
  .isUnique([3, 4], "Error: value already exists", flipLogic);
// .validate(); // you can call validate method directly after criteria definition or do it later as shown below

string

 simpleValidator
  .string("George", "stringValidator", "Error: value is not a string")
  .allowNull() // null values won't trigger errors
  .allowUndefined() // undefined values won't trigger errors
  .notRequired() // allowNull and allowUndefined combined
  .maxLength(3, "Error: maxLength>3", flipLogic)
  .minLength(3, "Error: minLength<3", flipLogic)
  .matchesRegex(new RegExp("foo*"), "Error: regex not matching", flipLogic)
  .email("This email is not valid", flipLogic)
  .zipCode("GR", true, false,
    "Invalid ZIP code",
    flipLogic) // you can ignore whitespaces so "383 33" = "38333" and decide what to do when there are missing zip code rules
  .startsWith("mouse", "Error: text doesn't start with such prefix", flipLogic)
  .endsWith("mouse", "Error: text doesn't end with such suffix", flipLogic)
  .contains("or", "Error: text doesn't contain such part", flipLogic)
  .phoneNumber("BG", "Error: invalid phone number", flipLogic)
  .equalTo("George", "Error: value !== comparissonValue", flipLogic)
  .isUnique(["George", "Mary"], "Error: value already exists", flipLogic);

object

  mainValidator
  .object({ users: [] }, "objectValidator", "Error: value is not an object") // defines new object sub validator
  .allowNull() // null values won't trigger errors
  .allowUndefined() // undefined values won't trigger errors
  .notRequired() // allowNull and allowUndefined combined
  .hasProp("users", "Error: object has no such prop", flipLogic)
  .isPropSet("car", "Error: value must be !== undefined && !== null", flipLogic)
  .notEmpty("Error: object has not props", flipLogic)
  .equalTo({}, "Error: value !== comparissonValue", flipLogic)
  .isUnique([obj1, obj2], "Error: value already exists", flipLogic);

boolean

mainValidator
  .boolean(true, "booleanVlidator", "Error: value is not a boolean") // defines new boolean sub validator
  .allowNull() // null values won't trigger errors
  .allowUndefined() // undefined values won't trigger errors
  .notRequired() // allowNull and allowUndefined combined
  .isTrue("Error: value !=true", flipLogic)
  .isFalse("Error: value !=false", flipLogic)
  .equalTo(true, "Error: value !== comparissonValue", flipLogic)
  .isUnique([true, false], "Error: value already exists", flipLogic);
get access to specific sub validator
const ageValidator = mainValidator.getValidator("ageValidator") // if the validator doesn"t exists it returns undefined
validate
//you can validate all sub validators at once by
mainValidator.validate();

// or to validate only specific validator using
const specificValidator = mainValidator.getValidator("ageValidator") // if the validator doesn"t exists it returns undefined
specificValidator.validate(); //this will automatically invoke onState changed function if available
specificValidator.validate(false); //this will NOT invoke onState changed function
errors
const someValidator = mainValidator.getValidator("someValidator");

someValidator.errors;// Returns errors array. This prop can be used on main validator or sub validators. Each error has name and message.
mainValidator.bulkErrors("someValidator1", "someValidator2", "someValidator3"); // combines the errors of multiple sub-validators

someValidator.hasErrors; // This prop can be used on main validator or sub validators
mainValidator.bulkHasErrors("someValidator1", "someValidator2"); // combines the result of multiple sub-validators

someValidator.firstErrorMessage; // retrieves the first error message
mainValidator.bulkFirstErrorMessage("someValidator1", "someValidator2"); // retrieves the first error message of the sub-validators group

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT