index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { getMetadataStorage } from './metadata/MetadataStorage';
  2. import { Validator } from './validation/Validator';
  3. import { getFromContainer } from './container';
  4. // -------------------------------------------------------------------------
  5. // Export everything api users needs
  6. // -------------------------------------------------------------------------
  7. export * from './container';
  8. export * from './decorator/decorators';
  9. export * from './decorator/ValidationOptions';
  10. export * from './validation/ValidatorConstraintInterface';
  11. export * from './validation/ValidationError';
  12. export * from './validation/ValidatorOptions';
  13. export * from './validation/ValidationArguments';
  14. export * from './validation/ValidationTypes';
  15. export * from './validation/Validator';
  16. export * from './validation-schema/ValidationSchema';
  17. export * from './register-decorator';
  18. export * from './metadata/MetadataStorage';
  19. /**
  20. * Validates given object by object's decorators or given validation schema.
  21. */
  22. export function validate(schemaNameOrObject, objectOrValidationOptions, maybeValidatorOptions) {
  23. if (typeof schemaNameOrObject === 'string') {
  24. return getFromContainer(Validator).validate(schemaNameOrObject, objectOrValidationOptions, maybeValidatorOptions);
  25. }
  26. else {
  27. return getFromContainer(Validator).validate(schemaNameOrObject, objectOrValidationOptions);
  28. }
  29. }
  30. /**
  31. * Validates given object by object's decorators or given validation schema and reject on error.
  32. */
  33. export function validateOrReject(schemaNameOrObject, objectOrValidationOptions, maybeValidatorOptions) {
  34. if (typeof schemaNameOrObject === 'string') {
  35. return getFromContainer(Validator).validateOrReject(schemaNameOrObject, objectOrValidationOptions, maybeValidatorOptions);
  36. }
  37. else {
  38. return getFromContainer(Validator).validateOrReject(schemaNameOrObject, objectOrValidationOptions);
  39. }
  40. }
  41. /**
  42. * Validates given object by object's decorators or given validation schema.
  43. * Note that this method completely ignores async validations.
  44. * If you want to properly perform validation you need to call validate method instead.
  45. */
  46. export function validateSync(schemaNameOrObject, objectOrValidationOptions, maybeValidatorOptions) {
  47. if (typeof schemaNameOrObject === 'string') {
  48. return getFromContainer(Validator).validateSync(schemaNameOrObject, objectOrValidationOptions, maybeValidatorOptions);
  49. }
  50. else {
  51. return getFromContainer(Validator).validateSync(schemaNameOrObject, objectOrValidationOptions);
  52. }
  53. }
  54. /**
  55. * Registers a new validation schema.
  56. */
  57. export function registerSchema(schema) {
  58. getMetadataStorage().addValidationSchema(schema);
  59. }
  60. //# sourceMappingURL=index.js.map