JSPM

  • Created
  • Published
  • Downloads 21997
  • Score
    100M100P100Q143532F
  • License MIT

Validator for adonis framework

Package Exports

  • @adonisjs/validator

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@adonisjs/validator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

AdonisJS Validator

Schema validator for AdonisJS

circleci-image typescript-image npm-image license-image

I have been maintaining indicative (used by this repo) for many years and learned a lot about the validation engines. This time I want to approach data validation from scratch and address lot of design issues that Indicative has and also squeeze out maximum performance this time.

Table of contents

Benchmarks

Let's compare the validator performance in comparison to some of the famous libraries in the Node.js Ecosystem.

  • Joi is one of the most famous and widely used schema validator for Node.js.
  • Class Validator is a popular library in the Typescript world, since it allows you to keep your static types in sync with the runtime validations.
  • Indicative My own work.

Textual Version

Benchmarking against flat object
AdonisJS x 6,777,738 ops/sec ±0.53% (81 runs sampled)
Joi x 705,094 ops/sec ±0.62% (81 runs sampled)
Indicative x 855,792 ops/sec ±0.30% (84 runs sampled)
Class Validator x 372,847 ops/sec ±0.38% (84 runs sampled)
Fastest is AdonisJS

Benchmarking against flat object with extra properties AdonisJS x 6,685,496 ops/sec ±0.41% (81 runs sampled) Joi x 445,545 ops/sec ±0.45% (83 runs sampled) Indicative x 836,625 ops/sec ±0.44% (84 runs sampled) Fastest is AdonisJS

Benchmarking against nested object AdonisJS x 4,742,486 ops/sec ±0.52% (82 runs sampled) Joi x 395,813 ops/sec ±0.48% (84 runs sampled) Indicative x 532,652 ops/sec ±0.27% (85 runs sampled) Class Validator x 216,392 ops/sec ±0.82% (83 runs sampled) Fastest is AdonisJS

Benchmarking against array of objects AdonisJS x 2,330,326 ops/sec ±0.42% (82 runs sampled) Joi x 297,187 ops/sec ±0.47% (82 runs sampled) Indicative x 394,948 ops/sec ±0.30% (83 runs sampled) Class Validator x 192,939 ops/sec ±1.25% (82 runs sampled) Fastest is AdonisJS

Goals

No code is the fastest code. In other words, making something fast is not a big deal, if you cut out all the features and compromise usability on every front.

I didn't started with making one of the fastest validation engines for Node.js. The goals were completely different and performance was just one of them.

  • Treat Typescript as a first class citizen. Runtime validations and static types should always be in sync. In other words, no need to write seperate interfaces for maintaining types.
  • Performance is important. Validating user input is a very common task every Web server has to perform and hence squeezing out more performance on this front is critical.
  • Do not mutate original data: The validator returns a new copy of data, holding only the validated properties. In the process, the original data object is never mutated.
  • Don't be stringent about errors format: Many validation libraries returns errors in a single hardcoded structure. If you need them in a different shape, then running a loop on the errors is the only option. With AdonisJS, it is as simple as creating an Error Formatter with couple of methods on it.

Usage

Docs will be added soon to the AdonisJS official website