@avonjs/avonjs
Version:
A fluent Node.js API generator.
54 lines (53 loc) • 1.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const helpers_1 = require("../helpers");
exports.default = (Parent) => {
class Nullable extends Parent {
/**
* Indicates if the field is nullable.
*/
acceptsNullValues = false;
/**
* Values which will be replaced to null.
*/
nullValidator = (value) => {
return (0, helpers_1.isNullish)(value);
};
/**
* Indicate that the field should be nullable.
*/
nullable(nullable = true, validator) {
this.acceptsNullValues = nullable;
if (validator !== undefined) {
this.nullValues(validator);
}
return this;
}
/**
* Specify nullable values.
*/
nullValues(nullValidator) {
this.nullValidator = nullValidator;
return this;
}
/**
* Determine if the field supports null values.
*/
isNullable() {
return this.acceptsNullValues;
}
/**
* Determine if the given value is considered a valid null value if the field supports them.
*/
isValidNullValue(value) {
return this.isNullable() && this.valueIsConsideredNull(value);
}
/**
* Determine if the given value is considered null.
*/
valueIsConsideredNull(value) {
return this.nullValidator(value);
}
}
return Nullable;
};