manifest
Version:
The backend for AI code editors
79 lines (78 loc) • 3.49 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationService = void 0;
const types_1 = require("../../../../types/src");
const common_1 = require("@nestjs/common");
const type_validators_1 = require("../records/type-validators");
const custom_validators_1 = require("../records/custom-validators");
let ValidationService = class ValidationService {
validate(itemDto, entityManifest, options) {
const errors = [];
if (options?.isUpdate) {
entityManifest.properties
.filter((p) => p.type === types_1.PropType.Password)
.forEach((p) => {
p.validation.isOptional = true;
});
}
entityManifest.properties.forEach((propertyManifest) => {
const propValue = itemDto[propertyManifest.name];
errors.push(...this.validateProperty(propValue, propertyManifest));
});
return errors;
}
validateProperty(propValue, propertyManifest) {
const errors = [];
if (typeof propValue !== 'undefined' && propValue !== null) {
const typeValidationError = type_validators_1.typeValidators[propertyManifest.type](propValue, propertyManifest.options);
if (typeValidationError) {
errors.push({
property: propertyManifest.name,
value: propValue,
constraints: {
type: typeValidationError
}
});
}
}
Object.entries(propertyManifest.validation || {}).forEach(([key, context]) => {
let validationError;
if (propertyManifest.validation.isOptional &&
(propValue === undefined || propValue === null)) {
validationError = null;
}
else {
validationError = custom_validators_1.customValidators[key](propValue, context);
}
if (validationError) {
const existingPropertyError = errors.find((error) => error.property === propertyManifest.name);
if (existingPropertyError) {
existingPropertyError.constraints = {
...existingPropertyError.constraints,
[key]: validationError
};
}
else {
errors.push({
property: propertyManifest.name,
value: propValue,
constraints: {
[key]: validationError
}
});
}
}
});
return errors;
}
};
exports.ValidationService = ValidationService;
exports.ValidationService = ValidationService = __decorate([
(0, common_1.Injectable)()
], ValidationService);