es-validation
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

es-validation

AppVeyor CircleCI Travis CI AppVeyor tests Codecov Code Climate Code Climate

Dependencies Development dependencies Known Vulnerabilities Greenkeeper

A set of constraint validation decorators inspired by JSR-303, JSR-349 and JSR-380.

The library makes it possible to:

  • define constraints on properties
  • define constraints on parameters
  • define constraints on returned values

Getting Started

The library can be installed using npm:

npm install es-validation --save

Or using yarn:

yarn add es-validation

Decorators

A number of decorators are available, and can be used on properties, parameters or functions. In the later case, the constraint is placed on the method's returned values.

String elements are converted to Boolean, Number or Date, depending on what type the constraint normally applies to. When relevant, Object elements are converted as well through toString().

Name Boolean Number String Date Map/Array Description
@AssertFalse ✔️ - ✔️ - - Must be false.
@AssertTrue ✔️ - ✔️ - - Must be true.
@NotNull ✔️ ✔️ ✔️ ✔️ ✔️ Must not be null.
@Null ✔️ ✔️ ✔️ ✔️ ✔️ Must be null.
@NotBlank - - ✔️ - - Must not be null and contain at least one non-space character.
@NotEmpty - - ✔️ - ✔️ Must not be empty (string length, array/map size).
@Size() - - ✔️ - ✔️ Must be within a size range (string length, array/map size).
@Pattern() - - ✔️ - - Must match a regular expression.
@Max() - ✔️ ✔️ - - Must be a number that is lower than or equal to a maximum.
@Min() - ✔️ ✔️ - - Must be a number that is higher than or equal to a minimum.
@PositiveOrZero - ✔️ ✔️ - - Must be a number that is higher than or equal to 0.
@Positive - ✔️ ✔️ - - Must be a number that is higher than 0.
@NegativeOrZero - ✔️ ✔️ - - Must be a number that is lower than or equal to 0.
@Negative - ✔️ ✔️ - - Must be a number that is lower than 0.
@Digits() - ✔️ ✔️ - - Must be a number with a set maximum of integral and fractional digits.
@FutureOrPresent - - ✔️ ✔️ - Must be a date set in the future or now.
@Future - - ✔️ ✔️ - Must be a date set in the future.
@PastOrPresent - - ✔️ ✔️ - Must be a date set in the past or now.
@Past - - ✔️ ✔️ - Must be a date set in the past.
@Email - - ✔️ - - Must be a valid email address.
@Valid - - - - ✔️ Properties must be validated (also applies to Object).

Validation

The module only provides an interface for validation, but no actual implementation. The implementation must be defined within a separate module.

The following known implementations are available:

Validator Factory

The ValidatorFactory interface is the entry-point for user code to obtain validator instances, mainly through the getValidator() method.

Validator

The Validator interface is used to perform constraint validation. The following methods are available:

Method Description
validate() Validate an object's properties.
validateProperty() Validate a specific object property.
validateValue() Validate a value.
validateParameters() Validate a function call's parameters.
validateReturnValue() Validate a function call's return value.

A Validator instance is obtained through a ValidatorFactory.

Date Provider

The DateProvider interface is used to retrieve the current time, which is necessary for various decorators (@Past, @Future, etc.). It contains a single method:

Method Description
getDate() Get the current time.

There is generally no interaction with this interface from user code, unless new custom decorators dealing with time are defined. In this scenario, the DateProvider instance can be obtained through ConstraintValidationContext's getDateProvider() method.

Development

The module can be built using the following command:

npm run build

It is also possible to keep unit tests executing as a background task:

npm run test

Readme

Keywords

none

Package Sidebar

Install

npm i es-validation

Weekly Downloads

2

Version

0.1.0

License

MIT

Unpacked Size

13.2 MB

Total Files

201

Last publish

Collaborators

  • rraziel