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

js-props-validator

v1.0.1

Published

JavaScript object properties validation.

Downloads

12

Readme

js-props-validator Build Status dependency status

JavaScript object properties validation. See below or src/_tests_/main-test.js for examples.

Getting Started

Install js-props-validator as NPM dependency.

npm install --save js-props-validator

Example

import Props from 'js-props';

const validator = Props.object({
    name: Props.string(),
    age: Props.number(),
    phone: Props.string().isOptional(),
    country: Props.oneOf([ 'Austria', 'Germany', 'Switzerland' ]).withDefault('Germany')
  });

validator.validate({
    name: 'Egon',
    age: 12
  });
// => true

validator.validate({
    name: 'Egon'
  });
// => Will throw exception due to missing age

validator.validateValue({
    name: 'Egon',
    age: 'twelve'
  });
// => false due to wrong type for age

validator.valueOrDefault({
    name: 'Egon',
  });
// => { name: 'Egon', country: 'Germany' }

Types

Any

Props.any([ ofType: Function | Array [, isOptional: boolean = false [, defaultValue: Any ]]])

Checks for any type. If ofType is a function it will be used to validate the value. E.g.:

const validator = Props.any((value) => value > 10);
validator.validateValue(13) === true;
validator.validateValue(9) === false;

If ofType is an array of Type the value will be checked if it is one of the given types. E.g.:

const validator = Props.any([ Props.number(), Props.string() ]);
validator.validateValue(12) === true;
validator.validateValue(twelve) === true;
validator.validateValue([ 12 ]) === false;

Array

Props.array([ ofType: Type [, isOptional: boolean = false [, defaultValue: Array ]]])

Checks for arrays. ofType can be defined as Type. E.g.:

const validator = Props.array(Props.string());
validator.validateValue([ "a", "b", "c" ]) === true;
validator.validateValue([ 1, 2, 3 ]) === false;

Boolean

Props.bool([ isOptional: boolean = false [, defaultValue: boolean ]])

Checks for boolean.

Enumeration

Props.oneOf(values: Array [, isOptional: boolean = false [, defaultValue: Any ]])

Checks for a set of defined values. E.g.:

const validator = Props.oneOf([ 1, 'A', 2 ]);
validator.validateValue(1) === true;
validator.validateValue('B') === false;

Function

Props.func([ isOptional: boolean = false [, defaultValue: Function ]])

Checks for functions.

Number

Props.number([ isOptional: boolean = false [, defaultValue: Number ]])

Checks for numbers.

Object

Props.object(ofType: Object | Type [, isOptional: boolean = false [, defaultValue: Number ]])

ofType can be a predefined object structure. E.g.:

const validator = Props.object({
    name: Props.string(),
    age: Props.number(),
    phone: Props.string().isOptional(),
    country: Props.oneOf([ 'Austria', 'Germany', 'Switzerland' ]).withDefault('Germany')
  });

validator.validate({
    name: 'Egon',
    age: 12
  }) === true;

If ofType is a type, the validator checks if every property of the object meets the type's validations. E.g.:

const validator = Props.object(Props.object({
    label: Props.string(),
    value: Props.any([ Props.number(), Props.string() ])
  }));

validator.validateValue({
    id: {
      label: "#",
      value: 0
    },
    name: {
      label: "Name",
      value: "Egon"
    }
  }) === true;

validator.validateValue({
    id: {
      value: 0
    },
    name: {
      label: "Name",
      value: "Egon"
    }
  }) === false;

String

Props.string([ isOptional: boolean = false [, defaultValue: String ]])

Checks for strings.

Type methods

Every type supports the following listed methods.

isOptional()

The validation will return true if value is not set.

const validator = Props.number().isOptional(); // alternative to: Props.number(true);
validator.validateValue(1) === true;
validator.validateValue(undefined) === true;
validator.validateValue("one") === false;

withDefault()

The validation will return a default value when calling valueOrDefault on a undefined value. Note: isOptional will be automatically set to true when calling withDefault.

const validator = Props.number().withDefault(42); // alternative to: Props.number(true);
validator.validateValue(1) === true;
validator.validateValue(undefined) === true;
validator.validateValue("one") === false;

validator.valueOrDefault(1) === 1;
validator.valueOrDefault(undefined) === 42;

validate(value: Any [, valueName: String])

The Method will validate the value, if the validation fails an error will be thrown. valueName is used within the error message if set.

const validator = Props.object({
    name: Props.string(),
    age: Props.number(),
    phone: Props.string().isOptional(),
    country: Props.oneOf([ 'Austria', 'Germany', 'Switzerland' ]).withDefault('Germany')
  });

validator.validate({
    name: 'Egon'
  });
// => Will throw exception due to missing age

validateValue(value: Any [, valueName: String])

Like validate but it will log an warning instead of throwing an error. Returns true if validation was successful, otherwise false.

valueOrDefault(value: Any)

Returns value or the default value when set and value === undefined. See example of withDefault.