@decaf-ts/decorator-validation
Version:
simple decorator based validation engine
63 lines • 2.52 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Validator } from "./Validator.js";
import { DEFAULT_ERROR_MESSAGES, ValidationKeys } from "./constants.js";
import { validator } from "./decorators.js";
import { isEqual } from "@decaf-ts/reflection";
/**
* @summary Equals Validator
*
* @param {string} [message] defaults to {@link DEFAULT_ERROR_MESSAGES#EQUALS}
*
* @class EqualsValidator
* @extends Validator
*
* @category Validators
*/
let EqualsValidator = class EqualsValidator extends Validator {
constructor(message = DEFAULT_ERROR_MESSAGES.EQUALS) {
super(message);
}
/**
* @summary Validates a model
*
* @param {string} value
* @param {EqualsValidatorOptions} options
* @param {PathProxy<any>} accessor - Proxy-like object used to resolve values from nested structures via path strings.
*
* @return {string | undefined}
*
* @override
* @see Validator#hasErrors
*/
hasErrors(value, options, accessor) {
let comparisonPropertyValue;
try {
comparisonPropertyValue = accessor.getValueFromPath(options[ValidationKeys.EQUALS]);
}
catch (e) {
return this.getMessage(e.message || this.message);
}
return isEqual(value, comparisonPropertyValue)
? undefined
: this.getMessage(options.message || this.message, options.label || options[ValidationKeys.EQUALS]);
}
};
EqualsValidator = __decorate([
validator(ValidationKeys.EQUALS),
__metadata("design:paramtypes", [String])
], EqualsValidator);
export { EqualsValidator };
// Validation.register({
// validator: EqualsValidator,
// validationKey: ValidationKeys.EQUALS,
// save: false,
// } as ValidatorDefinition);
//# sourceMappingURL=EqualsValidator.js.map