2.2.7 • Published 5 years ago

extensible-validator v2.2.7

Weekly downloads
5
License
ISC
Repository
github
Last release
5 years ago

extensible-validator

Typescript-friendly and extensible object and value validation.

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

2.2.7

5 years ago

2.2.6

5 years ago

2.2.5

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago