troi
v0.7.0
Published
Middleware-based data validation and sanitization library
Downloads
20
Readme
Troi
Troi is a middleware-based data validation and sanitization library.
Installation
npm install troi
Example
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: '[email protected]',
password: 'n0C0br4k4i'
});
// -> { username: 'karate-kid', email: '[email protected]', 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<any>)
unique()
between(min: number, max: number)
min(min: number)
max(max: number)
lengthBetween(min: number, max: number)
minLength(min: number)
maxLength(min: 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.
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.
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.
identity('string') // returns 'string'
Todos
- [ ] Allow middleware to pass
ValidationError
tonext()
and stop execution - [ ] Make chain builder immutable
- [ ] Add
coerce
transform function - [ ] Rename all
value
arguments toinput
for consistency - [ ] Use rollup.js