thorin.Interface.Sanitizer
You can write your own sanitization and validation functions by implementing
thorin.Interface.Sanitizer
and
calling thorin.addSanitizer()
Interface functionality
static code() : string
Must return the full upper-case code of the sanitizer. This is the code used together with
dispatcher.validate calls
static publicName() : string
Must return the publicly available name of the sanitizer. This is the value used when generating a validation error and
including the validator's public name
static aliases() : array[string]
You can define the alias codes your sanitizer will respond to, eg INTEGER -> [NUMBER, NUMERIC]
validate(input, opt)
This is the function that is called when a validation is done by your sanitizer. It will call with the
input data and the given set of options. (see notes)
-
input any the raw input value to sanitize
-
optobject additional options passed to the sanitizer
It is important to know
how your validation function returns data:
- If the validation fails, it must return a falsy value, preferably false
- If the validation passes, it must return:
- a promise and resolve it with an object with a value key, containing the sanitized input
- an object with a value key, containing the sanitized input
Example
'use strict';
class SanitizeString extends thorin.Interface.Sanitizer {
static code() { return 'STRING'; }
static publicName() { return 'String' }
static aliases() { return ['TEXT', 'VARCHAR'] }
validate(input, opt) {
if(typeof input !== 'string' || !string) return false;
// add additional processing steps
return {
value: input.trim()
}
}
}
thorin.addSanitizer(SanitizeString);