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

extensible-validator

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extensible-validator

Typescript-friendly and extensible object and value validation

  • 2.2.1
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

extensible-validator

Typescript-friendly and extensible object and value validation. It looks mostly like joi, but is more extensible and auto-complete friendly.

Also, unlike like other libraries where validate throws an error, I just return an array of errors, which could be empty. Throwing an error in the case of bad input from a function which exists to consider the case of bad input is ugly as hell.

I also separate validating and "casting" the input to a desired output format, because they're two separate operations.

Usage

Install:

$ npm install --save extensible-validator

Import, then define and use a schema:

import { object, string, number } from 'extensible-validator';

let validator = object.keys({
  name: string.required(),
  age: number.integer(),
});

let person = { name: 'Bob', age: 32 };
console.log(validator.validate(person));
// []

let notAPerson = { age: "what's my age again?" };
console.log(validator.validate(notAPerson));
// [{message: 'required', path: 'name'}, {message: 'must be a number', path: 'age'}]

If you want to extend the validator functionality, you can either add tests to existing types:

const objectId = string.addTest(value => ObjectId.isValid(value));

const person = object.keys({
  _id: objectId.required(),
  name: string.required(),
});

Or you can define a new schema class:

class ObjectIdSchema extends Schema<ObjectId> {
  constructor() {
    super({ type: 'must be an ObjectId' }, value => ObjectId.isValid(value));
  }

  cast(value: any) {
    return value instanceof ObjectId ? value : new ObjectId(value);
  }
}

The latter gives you more control, and the ability to override the type test and the cast method.

TODO

Keywords

FAQs

Package last updated on 17 Jun 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