Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

is-okay

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

is-okay

Simple and fast type validator

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

IsOkay Validator

Simple

const isOkay = require('is-okay');

const v = isOkay();

v.required('botId')
    .string()
    .is('not a reseved word [app]', b => b !== 'app')
    .is('max 47 chars long', b => b.length <= 47);

v.required('wingbotToken')
    .string();

v.optional('tier')
    .default('free')
    .is('one of allowed values', t => ['free', 'staging', 'production'].includes(t));

const data = v.okay(inputData);

Validates nested objects

const isOkay = require('is-okay');

const v = isOkay();

v.nullable('opt');
v.required('opt.req').string();

assert.deepEqual(v.okay({}), { opt: null });

assert.throws(() => {
    v.okay({
        opt: {}
    });
});

assert.deepEqual(v.okay({
    opt: { req: 'a' }
}), { opt: { req: 'a' } });

Reuse the validator for MongoDB updates

All root keys of input will be treated as optional.

const isOkay = require('is-okay');

const v = isOkay();

v.nullable('opt');
v.required('opt.req').string();

v.required('id');

const input = {};

assert.deepEqual(v.okay(input, true), {});

Objects in arrays

const isOkay = require('is-okay');

const v = isOkay();

v.nullable('some.nested');
v.nullable('array[].value');
v.optional('array[].opt').default(1);
v.optional('array[].notHere');
v.required('array[].required');
v.required('required');

v.nullable('some.nested');
v.nullable('array[].value')
    .string();
v.optional('array[].opt')
    .default(1);
v.optional('array[].notHere');
v.required('array[].required')
    .string();
v.required('required')
    .number();

assert.deepEqual(v.okay({
    required: 1,
    notHere: 2,
    array: [
        { required: 'abv', removeMe: 4, value: null },
        { required: 'abc', out: 6, opt: 1 }
    ]
}), {
    required: 1,
    some: { nested: null },
    array: [
        { required: 'abv', opt: 1, value: null },
        { required: 'abc', opt: 2, value: null }
    ]
});


API

Classes

Rule

{Rule} Validation configurator

Typedefs

ValidationError : Error
validator : function

Validator callback

Rule

{Rule} Validation configurator

Kind: global class

rule.string() ⇒ this

Sets filter

Kind: instance method of Rule

rule.number() ⇒ this

Sets filter

Kind: instance method of Rule

rule.boolean() ⇒ this

Sets filter

Kind: instance method of Rule

rule.default(defaultValue) ⇒ this

Sets default value

Kind: instance method of Rule

ParamType
defaultValue*

rule.is(message, fn) ⇒ this

Adds validator

Kind: instance method of Rule

ParamType
messagestring
fnvalidator

rule.notEmpty() ⇒ this

Value should not be empty (not falsey)

Kind: instance method of Rule

ValidationError : Error

Kind: global typedef
Properties

NameType
invalidKeystring
statusnumber
statusCodenumber

validator : function

Validator callback

Kind: global typedef

ParamTypeDescription
value*found value
key*a key, where the value was found

Keywords

FAQs

Package last updated on 23 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc