https://github.com/neworbit/not-valid
Extensible and flexible TypeScript validation
https://github.com/neworbit/not-valid
hacktoberfest javascript nodejs package typescript validation
Last synced: 7 months ago
JSON representation
Extensible and flexible TypeScript validation
- Host: GitHub
- URL: https://github.com/neworbit/not-valid
- Owner: NewOrbit
- License: mit
- Created: 2017-05-08T10:42:28.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-05-23T10:18:06.000Z (about 3 years ago)
- Last Synced: 2025-01-30T12:42:05.858Z (over 1 year ago)
- Topics: hacktoberfest, javascript, nodejs, package, typescript, validation
- Language: TypeScript
- Homepage:
- Size: 92.8 KB
- Stars: 3
- Watchers: 30
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# not-valid
Composable message-based validation
## Installation
$ npm install not-valid --save
## Usage
### Overview
```typescript
import { validate } from "not-valid";
// Use createValidator to specify a rule and the message given for breaking that rule
const mustContainA = createValidator(v => v.indexOf("A") !== -1, "Value must contain the letter 'a'");
// pass in array of validation functions and a value to validate
validate([ mustContainA ], "cheese"); // [ "Value must contain the letter 'a'" ] - returns error messages
// can use a factory pattern for your validation methods to make things nice
const mustContain = (requirement: any) => {
return createValidator>(v => v.indexOf(requirement) !== -1, `Value must contain '${requirement}'`);
};
const lengthWithinBounds = (min: number, max: number) => {
return createValidator(v => v.length < min || v.length > max, `Value must have length between ${min} and ${max}`);
};
// you can pass in multiple validators
validate([
mustContain("Z"),
lengthWithinBounds(2, 6)
], "Too long a string, they say!"); // [ "Value must contain 'Z'", "Value must have length between 2 and 6" ]
```
### Validators
A number of validation functions come bundled with this package. You can use them like so:
```typescript
import { validators } from "not-valid";
validate([
validators.validLength({ min: 6, max: 12 })
], "Good value");
```
The validators included are as follows:
- `requiredString`
- `requiredNumber`
- `validLength`
- `validEmail`
- `validAlphaNumeric`
- `validOption`
- `validPhoneNumber`
- `validNINumber`
- `validUKDrivingLicence`
- `validSortCode`
- `validBankAccountNumber`
- `validVATNumber`
### Creating validation functions
A validation function must take in a value `value`, and return `Result.Pass` if `value` is valid, or `Result.Fail(message)` is `value` is invalid. They can also return `Result.Stop`, which will silently stop the validation cycle (no more errors).
This can be done with the helper method `createValidator` in `not-valid`:
```typescript
import { createValidator } from "not-valid";
const mustContainA = createValidator(v => v.indexOf("A") !== -1, "Value must contain the letter 'a'");
```
You can use factory patterns around this to make it nicer:
```typescript
const mustContain = (requirement: any) => {
return createValidator>(v => v.indexOf(requirement) !== -1, `Value must contain '${requirement}'`);
};
```
All validators (except for validators that explicitly check for "required") should treat empty string, null and undefined
as valid.
This is because we can combine validators with "required" validators in order to enforce something being valid and not empty,
but also allows us to accept nothing being entered if desired.
### Options
The third parameter of `validate` is an object containing options.
```typescript
interface ValidationOptions {
sequential?: boolean;
}
```
#### `sequential`
Default: `true`
The validation will break on the first error, therefore only returning a single validation error.
```typescript
validate([ something, another ], 5, { sequential: false });
```
If `something` fails validation, `another` will not be called.
## License
Made with :sparkling_heart: by [NewOrbit](https://www.neworbit.co.uk/) in Oxfordshire, and licensed under the [MIT Licence](LICENCE)