https://github.com/barry127/sontaran
Javascript validator for Array, Boolean, Number, Object, String
https://github.com/barry127/sontaran
javascript javascript-validation
Last synced: 3 months ago
JSON representation
Javascript validator for Array, Boolean, Number, Object, String
- Host: GitHub
- URL: https://github.com/barry127/sontaran
- Owner: Barry127
- License: mit
- Created: 2016-12-28T13:36:25.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-09-24T19:37:21.000Z (7 months ago)
- Last Synced: 2024-10-01T09:13:58.888Z (7 months ago)
- Topics: javascript, javascript-validation
- Language: TypeScript
- Homepage: https://barry127.github.io/sontaran/
- Size: 1.92 MB
- Stars: 0
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Sontaran
[](https://travis-ci.com/Barry127/sontaran)
[](https://coveralls.io/github/Barry127/sontaran?branch=master)Sontaran is a javascript validator library. It has a lot validation options out of the box and all validators are extendable with custom validation functions.
Some key features
- Completely written in Typescript
- CommonJS build for Node JS
- Tree shakable ES build
- Fluid, chainable api
- Support for async validators with the `validateAsync` method## Installation
Sontaran can be installed using npm.
```bash
npm install --save sontaran
```For the complete code including all tests the repo can be cloned.
```bash
git clone https://github.com/Barry127/sontaran.git
cd sontaran
npm run test
```## Getting Started
The `object().schema()` function takes a schema of Sontaran validators as an argument.
In this example:
**username**
- Must be a string
- Cannot be only empty characters (spaces, tabs, return, ...)
- Must have a length between 3 and 10 characters
- Must match the given RegExp (only contain alphanumeric characters and dash, underscore)**email**
- Must be a valid email
**password**
- Must be a string
- Must have a length of at least 8 characters```javascript
import { object, string, email } from 'sontaran';const schema = object().schema({
username: string()
.notEmpty()
.between(3, 10)
.match(/^[a-zA-Z0-9_\-]*$/),
email: email(),
password: string().min(8)
});// Valid schema (return true)
schema.validate({
username: 'sontaran',
email: '[email protected]',
password: 'mySuperSecretPassword'
}); /** => {
valid: true,
value: {
username: 'sontaran',
email: '[email protected]',
password: 'mySuperSecretPassword
}
}*/// invalid usernames
let a = 123; // => not a string
let b = ' \t\r'; // => Empty characters
let c = 'aa'; // => too short
let d = 'Hello-World'; // => too long
let e = 'B@dInput'; // => invalid character
```Sontarans `custom` can take any `ValidatorFunction` function as argument to validate and / or transform a value.
```javascript
import { string, ValidationError } from 'sontaran';// value cannot be root and is transformed to lowercase
const myCustomValidator = (value) => {
if (value.toLocaleLowercase() === 'root') {
throw new ValidationError('root is not allowed');
}return value.toLocaleLowercase();
};const schema = string().custom(myCustomValidator);
// valid result transformed to lowercase
const result = schema.label('username').validate('Admin');
/* => {
valid: true,
value: 'admin'
}*/// invalid result
const result = schema.label('username').validate('Root');
/* => {
valid: false,
value: 'Root',
errors: [{
field: 'username',
message: 'root is not allowed',
type: 'root is not allowed'
}]
}
*/
```