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

nm-validator

v1.0.18

Published

Simple json validator by using user-defined validation rules

Downloads

69

Readme

nm-validator

This package is DEPRECATED. The REPLACEMENT for is: https://www.npmjs.com/package/ts-validity

Installation

npm i nm-validator

or

yarn add nm-validator

Usage

Example 1 : Validate Object

import { validator } from "nm-validator";
import {
  emailAddress,
  equal,
  minLength,
  required,
  regularExpression,
} from "nm-validator/dist/validationRules";

const company = {
  name: "",
  email: "irpan2gmail.com",
  address: {
    streetName: "",
    country: "UK",
    person: {
      age: 15,
    },
  },
};

//Deep validation
const rules: ValidationRules<typeof company> = {
  name: [required("Name is required")],
  address: {
    streetName: [required("The street name is required")],
    country: [elementOf(["US,FR,JP,ID"])],
    person: {
      age: [minNumber(17)],
    },
  },
};

const validationResult = validator.validateObject(registrant, validationRule);
//Output validationResult:
// validationResult = {
//   isValid: false,
//   errorMessages: {
//     name: ["Name is required."],
//     address: {
//       streetName: ["The street name is required."],
//       country: ["The value 'UK' is not the element of [US,FR,JP,ID]."],
//       person: {
//         age: ["The minimum value for this field is 17."],
//       },
//     },
//   },
//   errors: {
//     name: "Name is required.",
//     address: {
//       streetName: "The street name is required.",
//       country: "The value 'UK' is not the element of [US,FR,JP,ID].",
//       person: {
//         age: "The minimum value for this field is 17.",
//       },
//     },
//   },
// };

Example 2 : Validate a Spesific Field

import { validator } from "nm-validator";
import {
  emailAddress,
  equal,
  minLength,
  required,
  regularExpression,
} from "nm-validator/dist/validationRules";

const registrant: Registrant = {
  name: "",
  email: "",
  password: "",
  confirmPassword: "",
};

const validationRule: ValidationRules<Registrant> = {
  name: [required("Name is required"), minLength(3, "The minimum length is 3")],
  email: [required("Email is required"), emailAddress("Invalid email address")],
};

//only validate the email field
const validationResult = validator.validateField(
  registrant,
  "email",
  validationRule
);
//Output
// validationResult = {
//     isValid: false,
//     errorMessages: {
//         email: [
//             "Email is required",
//             "Invalid email address"
//         ]
//     },
//     errors: {
//         email: "Email is required. Invalid email address."
//     }
// }

Example 3 : Create and use your own field rule

import { validator } from "nm-validator";

//This is our custom field rule, should return the FieldValidator interface
const mustBePi: FieldRule = (errorMessage?: string) => {
  const c = 3.14;
  let msg = `The value must be ${c}`;
  if (errorMessage) {
    msg = errorMessage;
  }
  const validator: FieldValidator = {
    errorMessage: msg,
    validate: (value: any, objRef?: any): boolean => {
      return value === c;
    },
  };
  return validator;
};

const pi = {
  value: 3.13,
};

const validationRule: ValidationRules = {
  value: [mustBePi()],
};

const validationResult = validator.validateObject(pi, validationRule);

//Output
// validationResult = {
//   isValid: false,
//   errorMessages: {
//     value: ["The value must be 3.14"],
//   },
//   errors: {
//     value: "The value must be 3.14",
//   },
// };

Example 4 : Deep validate spesific field

import { validator } from "nm-validator";
import {
  emailAddress,
  equal,
  minLength,
  required,
  regularExpression,
} from "nm-validator/dist/validationRules";

const company = {
  name: "",
  email: "irpan2gmail.com",
  address: {
    streetName: "",
    country: "UK",
    person: {
      age: 15,
    },
  },
};

const rules: ValidationRules<typeof company> = {
  name: [required("Name is required")],
  address: {
    streetName: [required("The street name is required")],
    country: [elementOf(["US,FR,JP,ID"])],
    person: {
      age: [minNumber(17)],
    },
  },
};

//Validate only the age value 
const actual = validator.validateField(company, "address.person.age", rules);

//Output
//  validationResult = {
//   isValid: false,
//   errorMessages: {
//     address: {
//       person: {
//         age: ["The minimum value for this field is 17."],
//       },
//     },
//   },
//   errors: {
//     address: {
//       person: {
//         age: "The minimum value for this field is 17.",
//       },
//     },
//   },
// };