Search results

Types

Classes
Interfaces
Enums
Functions
Type aliases
Constants

Members

Properties
Methods
Getters
Setters
Enum members
Show privates

Other

In this module only

Getting started

Objvl is a fast and safe object validator for javascript and typescript. It compiles your schemas to easily optimizable javascript code!

Install

npm i objvl

Basic schema

A "schema" describes how the object should look. Every schema has a list of properties, and each of them has a typetype typecompiler/SchemaType. You can write schemas in two ways: the builder pattern, which is very similar to joi, or via objects, which is similar to ajv.

The following code creates a function which makes sure an object you pass to it has:

  • The property "name", which must be a string, with maximum 52 characters in it.
  • The property "age", which is an integer, with minimum value of 13.

By default, both properties are required. You can make them optional by setting optional to true, and as you can see, we handle all the possible errors ourselves. objvl has default errors built in, but it's always a good practice to handle the errors yourself.

BuildersObjects

You can also not create string errors, and instead just use numbers, which specify the type of the error, and let the client handle them. The following example just sends numbers, but you could send extra data such as the name of the field that the error comes from, and the actual value received.

const enum Errors {
    MustBeStr,
    MaxLen
}
BuildersObjects

This way you decide how the errors should look, so there's no overhead - objvl doesn't create error objects, from which you only need one piece of information.

Objvl reduces error overhead by inlining arrow functions which have a single expression as their body!

Errors

By default, objvl gives you all the errors it can get, one error per property. For example:

BuildersObjects