@ahs502/validation

A universal client/server side data model validation.

Usage no npm install needed!

<script type="module">
  import ahs502Validation from 'https://cdn.skypack.dev/@ahs502/validation';
</script>

README

@ahs502/validation

A universal client/server side data model validation.

Have you ever struggled validating forms with complex and dynamic data structures?

Have you ever tried to validate each field from different aspects and generate accurate error messages on each one telling exactly what is wrong with that field?

Are you tired from those stupid rule definitions and wish to be able to freely apply all sorts of custom checks on your data directly by JavaScript native expressions?

Did you have a hard time to implement cross field validations or sequential/conditional checks on your data?

Have you ever thought about implementing validation on your data model once and use it both within the client and the server side code?

If the answer is yes, then @ahs502/validation system is here to save the day!

  • Very simple, yet very powerful
  • No rules, no conventions, just plain JavaScript expressions to check things
  • Full TypeScript support, as well as JavaScript
  • Support for asynchronous, synchronous and mixed checks
  • A lot of freedom to check things in all sorts of ways
  • Capability to apply complex logics and arbitrary orders to check everything
  • Easy to learn, enjoyful to implement

All you need to import is:

import Validation from '@ahs502/validation'

In TypeScript:

interface Point {
  x: number
  y: number
}

class PointValidation extends Validation<
  'X_IS_VALID' | 'Y_IS_VALID' | 'IS_WITHIN_RANGE'
> {
  constructor({ x, y }: Point, range: number) {
    super(validator => {
      validator.check('X_IS_VALID', !isNaN(x), 'x is invalid.')
      validator.check('Y_IS_VALID', !isNaN(y), 'y is invalid.')
      validator
        .when('X_IS_VALID', 'Y_IS_VALID')
        .check(
          'IS_WITHIN_RANGE',
          () => Math.sqrt(x ** 2 + y ** 2) <= range,
          'The point is out of range.'
        )
    })
  }
}

Or, in JavaScript:

class PointValidation extends Validation {
  constructor({ x, y }, range) {
    super(validator => {
      validator.check('X_IS_VALID', !isNaN(x), 'x is invalid.')
      validator.check('Y_IS_VALID', !isNaN(y), 'y is invalid.')
      validator
        .when('X_IS_VALID', 'Y_IS_VALID')
        .check(
          'IS_WITHIN_RANGE',
          () => Math.sqrt(x ** 2 + y ** 2) <= range,
          'The point is out of range.'
        )
    })
  }
}

The usage is like:

const p1 = { x: 1, y: 2 }
const v1 = new PointValidation(p1, 10)
v1.ok           // true
v1.badges       // ['X_IS_VALID', 'Y_IS_VALID', 'IS_WITHIN_RANGE']
v1.failedBadges // []
v1.messages()   // []
const p2 = { x: -3, y: NaN }
const v2 = new PointValidation(p2, 10)
v2.ok           // false
v2.badges       // ['X_IS_VALID']
v2.failedBadges // ['Y_IS_VALID']
v2.messages()   // ['y is invalid.']
const p3 = { x: 100, y: 100 }
const v3 = new PointValidation(p3, 10)
v3.ok           // false
v3.badges       // ['X_IS_VALID', 'Y_IS_VALID']
v3.failedBadges // ['IS_WITHIN_RANGE']
v3.messages()   // ['The point is out of range.']

Installation

$ npm install @ahs502/validation

Documentation

This documentation contains tutorials, examples and API details.

Development

Run tests (Powered by jest):

$ npm test

Build the project into the dist folder:

$ npm run build

It's recommended to use VS Code IDE with Prettier extension installed.

Contribution

It would be appreciated if you had any suggestion or contribution on this package or detected any bug.