The execution handler is a function that is used to set the changes on the
target object. By default, this is Object.assign
. You can specify a custom
execution handler; for example, you could use Lodash's assign
, Ember's
set
, and so forth:
const proxy = validatedProxy(original, {
executionHandler: (target, changes) => { // do something here },
validations: // ...
});
A validation map is an object containing the mapping between the target schema and validator functions.
Function signature for validator functions. Pass in a type parameter here to
allow typechecking newValue
.
Shorthand for Object.prototype.hasOwnProperty
. Returns a boolean indicating
whether the object has the specified property as own (not inherited)
property. Useful when the object was created with Object.create(null)
.
hasOwnProperty({ foo: 1 }, 'foo'); // true
Wraps a target object with a BufferedProxy
. Setters will first invoke a
validator function, if found in a supplied validation map. The validator
function returns a ValidatedResult
, and the BufferedProxy
is set. Getters
will forward to the target object, first returning the error, then the
change, and finally the original value;
If the result is valid, the value is set into the BufferedProxy
's internal
value cache.
If the result is invalid, the value is set into the BufferedProxy
's internal
error cache.
const user = {
name: 'Billy Bob',
age: 25
};
const bufferedProxy = validatedProxy(user, {
validations: {
name: [
validatePresence(true),
validateLength({ min: 4 })
],
age: [
validatePresence(true),
validateNumber({ gte: 18 })
]
}
});
bufferedProxy.name = 'Michael Bolton';
user.name; // 'Billy Bob'
bufferedProxy.flush();
user.name; // 'Michael Bolton'
Looks up validator function(s) from a validator map. If none are found, fall
back to the defaultValidator
.
const original = { foo: null };
const validationMap = {
foo: validatePresence(),
bar: [
validatePresence(),
validateLength({ gt: 2 })
]
};
validatorLookup(validationMap, 'foo'); // ValidatorFunction[]
Generated using TypeDoc
The error handler is a function that is called when an invalid value is set. This is in addition to the error *that is already stored in the cache. By default, the error handler is a
no-op
(does nothing). You can specify a custom error handler; for example, you could log error messages, send them to a server, etc:const proxy = validatedProxy(original, { errorHandler: errorMessages => { // do something here }, validations: // ... });