The Validator is designed to validate objects against constraints. They are assertions that a condition is true.
Basic constraints:
String constraints:
Comparison constraints:
Number constraints:
Date constraints:
Choice constraints:
Financial and other Number Constraints:
Other constraints:
import { AbstractConstraint } from 'constraint-validator';
export default class MyAwesomeConstraint extends AbstractConstraint {
/**
* @return {{message: string}}
*/
getDefaultOptions() {
return {
// provide list of default configuration values
'message': 'My Awesome constraint failed',
};
}
/**
* @return {string[]}
*/
getRequiredOptions() {
// provide list of required configuration options
return ['my_option'];
}
/**
* Test provided value and return Error if occurs
*
* @param value
* @param {{}} [options]
*
* @return {Error|undefined}
*/
validate(value, options = {}) {
// provide your custom validation logic
let isValid = false;
/**
* hack, hack, hack ...
* isValid => true || false
*/
if (!isValid) {
// build validation error
return this
.getViolationBuilder()
.setParameter('value', value)
.build(this.options.message);
}
}
}