Validator.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Validator = void 0;
  4. const ValidationExecutor_1 = require("./ValidationExecutor");
  5. /**
  6. * Validator performs validation of the given object based on its metadata.
  7. */
  8. class Validator {
  9. /**
  10. * Performs validation of the given object based on decorators or validation schema.
  11. */
  12. validate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) {
  13. return this.coreValidate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions);
  14. }
  15. /**
  16. * Performs validation of the given object based on decorators or validation schema and reject on error.
  17. */
  18. async validateOrReject(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) {
  19. const errors = await this.coreValidate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions);
  20. if (errors.length)
  21. return Promise.reject(errors);
  22. }
  23. /**
  24. * Performs validation of the given object based on decorators or validation schema.
  25. */
  26. validateSync(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) {
  27. const object = typeof objectOrSchemaName === 'string' ? objectOrValidationOptions : objectOrSchemaName;
  28. const options = typeof objectOrSchemaName === 'string' ? maybeValidatorOptions : objectOrValidationOptions;
  29. const schema = typeof objectOrSchemaName === 'string' ? objectOrSchemaName : undefined;
  30. const executor = new ValidationExecutor_1.ValidationExecutor(this, options);
  31. executor.ignoreAsyncValidations = true;
  32. const validationErrors = [];
  33. executor.execute(object, schema, validationErrors);
  34. return executor.stripEmptyErrors(validationErrors);
  35. }
  36. // -------------------------------------------------------------------------
  37. // Private Properties
  38. // -------------------------------------------------------------------------
  39. /**
  40. * Performs validation of the given object based on decorators or validation schema.
  41. * Common method for `validateOrReject` and `validate` methods.
  42. */
  43. coreValidate(objectOrSchemaName, objectOrValidationOptions, maybeValidatorOptions) {
  44. const object = typeof objectOrSchemaName === 'string' ? objectOrValidationOptions : objectOrSchemaName;
  45. const options = typeof objectOrSchemaName === 'string' ? maybeValidatorOptions : objectOrValidationOptions;
  46. const schema = typeof objectOrSchemaName === 'string' ? objectOrSchemaName : undefined;
  47. const executor = new ValidationExecutor_1.ValidationExecutor(this, options);
  48. const validationErrors = [];
  49. executor.execute(object, schema, validationErrors);
  50. return Promise.all(executor.awaitingPromises).then(() => {
  51. return executor.stripEmptyErrors(validationErrors);
  52. });
  53. }
  54. }
  55. exports.Validator = Validator;
  56. //# sourceMappingURL=Validator.js.map