https://github.com/envelope/troi
Middleware-based data validation and sanitization library
https://github.com/envelope/troi
javascript sanitize validation
Last synced: 11 months ago
JSON representation
Middleware-based data validation and sanitization library
- Host: GitHub
- URL: https://github.com/envelope/troi
- Owner: envelope
- License: mit
- Created: 2020-09-11T14:00:07.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2025-02-22T13:26:01.000Z (over 1 year ago)
- Last Synced: 2025-04-10T00:43:37.437Z (about 1 year ago)
- Topics: javascript, sanitize, validation
- Language: JavaScript
- Homepage:
- Size: 103 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Troi
Troi is a middleware-based data validation and sanitization library.
[](https://travis-ci.org/envelope/troi)
[](https://www.npmjs.com/package/troi)
## Installation
```
npm install troi
```
## Example
```JavaScript
const troi = require('troi/chain');
const schema = troi.object({
username: troi.filled()
.string()
.trim()
.lengthBetween(2, 10),
email: troi.filled()
.lowercase()
.email(),
password: troi.filled()
.string()
.lengthBetween(3, 30)
.pattern(/^[a-zA-Z0-9]+$/)
});
schema.validate({
username: 'karate-kid',
email: 'larusso@example.org',
password: 'n0C0br4k4i'
});
// -> { username: 'karate-kid', email: 'larusso@example.org', password: 'N0C0br4k4i' }
schema.validate({
username: 'karate-kid'
});
// Throws validation error
```
### API
Work in progress
#### `filled()`
#### `required()`
#### `optional()`
#### `nullable()`
#### `string()`
#### `number()`
#### `integer()`
#### `boolean()`
#### `date()`
#### `array(itemValidator?: function)`
#### `object(properties?: object)`
#### `params(schema: object)`
#### `oneOf(values: Array)`
#### `unique()`
#### `between(min: number, max: number)`
#### `min(min: number)`
#### `max(max: number)`
#### `lengthBetween(min: number, max: number)`
#### `minLength(min: number)`
#### `maxLength(min: number)`
#### `length(expectedLength: number)`
#### `pattern(regexp: RegExp, type?: string)`
#### `email()`
#### `lowercase()`
#### `uppercase()`
### Chain API
Work in progress
## Concepts and Terminology
### Middleware Function
Middleware functions can perform the following tasks:
- return validation errors and stop further execution
- transform values (examples: `trim`, `nullify`)
- intercept execution (examples: `optional`, `nullable`)
### Transform function
A middleware function that *may* transform its `input` argument and return another value.
```JavaScript
const trim = (input, next) => next(typeof input === 'string' ? input.trim() : input);
```
### Interceptor function
A middleware function that *may* stop the execution of remaining middleware functions.
```JavaScript
const optional = (input, next) => input === undefined ? input : next(input)
```
### Identity Function
A function that always returns the same value that was used as its argument.
```JavaScript
identity('string') // returns 'string'
```
## Todos
- [ ] Allow middleware to pass `ValidationError` to `next()` and stop execution
- [ ] Make chain builder immutable
- [ ] Add `coerce` transform function
- [ ] Rename all `value` arguments to `input` for consistency
- [ ] Use rollup.js