1.0.3 • Published 7 months ago

@edward644/validate v1.0.3

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

Validation Library

A validation library that ensures your data structures match specified types or schemas during runtime, making it perfect for handling dynamic or external data sources such as APIs, user inputs, or databases.

Getting Started

Installation

Install the library using npm:

npm install @edward644/validate

Usage

Import the library and start validating:

CJS

const v = require('@edward644/validation').default;

v(123).isNumber().isGreaterThan(0).isLessThan(1000).assert(); // Throws error if validation fails

ESM

import v from '@edward644/validate';

v(123).isNumber().isGreaterThan(0).isLessThan(1000).assert(); // Throws error if validation fails
import v from '@edward644/validate';

v(123).isNumber().isGreaterThan(0).isLessThan(1000).isValid; // Returns boolean
import v from '@edward644/validate';

const vUser = data =>
  v(data).matchesSchema({
    name: e => e.isString().isNotEmpty(),
    age: e => e.isNumber().isGreaterThan(0).optional(),
    email: e => e.isString().isEmail(),
    active: e => e.isBoolean(),
    accounts: e => e.isArray().isNotEmpty().each(i => i.isString().isNotEmpty()),
  });

const user = {
  name: 'John Doe',
  age: 30,
  email: 'john.doe@example.com',
  active: true,
  accounts: ['savings', 'checking']
}

vUser(user).isValid;
vUser(user).assert();

Build and Test

Build

To build the library, run:

npm run build

The compiled files will be located in the dist directory.

Test

Run the tests using Jest:

npm test

License

This project is licensed under the ISC License.

References

1.0.3

7 months ago

1.0.2

7 months ago

1.0.1

7 months ago

1.0.0

7 months ago

0.0.0-beta.3

7 months ago

0.0.0-beta.2

7 months ago

0.0.0-beta.1

7 months ago

0.0.0-beta.0

7 months ago