kanun 1.1.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1674,13 +1674,17 @@ function registerImplicit(rule, validate, replaceMessage) {
1674
1674
  //#endregion
1675
1675
  //#region src/Plugin.ts
1676
1676
  const valueInspectors = [];
1677
+ const validationErrorHooks = [];
1678
+ const validationSuccessHooks = [];
1677
1679
  const api = {
1678
1680
  registerRule: register,
1679
1681
  registerImplicitRule: registerImplicit,
1680
1682
  registerValueInspector,
1681
1683
  extendTranslations: (translations) => {
1682
1684
  Lang.extendTranslationObject(translations);
1683
- }
1685
+ },
1686
+ onValidationError: registerValidationErrorHook,
1687
+ onValidationSuccess: registerValidationSuccessHook
1684
1688
  };
1685
1689
  function definePlugin(plugin) {
1686
1690
  return plugin;
@@ -1696,6 +1700,18 @@ function registerValueInspector(inspector) {
1696
1700
  }
1697
1701
  valueInspectors.push(inspector);
1698
1702
  }
1703
+ function registerValidationErrorHook(hook) {
1704
+ validationErrorHooks.push(hook);
1705
+ }
1706
+ function registerValidationSuccessHook(hook) {
1707
+ validationSuccessHooks.push(hook);
1708
+ }
1709
+ async function dispatchValidationErrorHooks(validator) {
1710
+ for (const hook of validationErrorHooks) await hook(validator);
1711
+ }
1712
+ async function dispatchValidationSuccessHooks(validator) {
1713
+ for (const hook of validationSuccessHooks) await hook(validator);
1714
+ }
1699
1715
  function getValidationValueInspector(value) {
1700
1716
  return valueInspectors.find((inspector) => inspector.matches(value));
1701
1717
  }
@@ -3392,6 +3408,8 @@ var Validator = class Validator {
3392
3408
  if (this.executed) return this._errors.isEmpty();
3393
3409
  const exec = await this.execute();
3394
3410
  for (const after of this.#after) after();
3411
+ if (exec.passing) await dispatchValidationSuccessHooks(this);
3412
+ else await dispatchValidationErrorHooks(this);
3395
3413
  return exec.passing;
3396
3414
  }
3397
3415
  /**
@@ -3685,4 +3703,4 @@ var ValidationException = class ValidationException extends Error {
3685
3703
  }
3686
3704
  };
3687
3705
  //#endregion
3688
- export { BaseRule, BaseValidator, ErrorBag, ExtendedRules, IDatabaseDriver, ImplicitRule, Lang, MessageBag, Password, ValidationException, ValidationRule, ValidationServiceProvider, Validator, addImplicitRule, buildValidationMethodName, compare, convertValuesToBoolean, convertValuesToNull, convertValuesToNumber, deepEqual, deepFind, deepFindMessage, deepSet, definePlugin, dotify, getFormattedAttribute, getKeyCombinations, getMessage, getNumericRules, getSize, getValidationMessageType, getValidationSize, getValidationValueInspector, getValidatorContext, isArrayOfRules, isImplicitRule, isInteger, isNumericRule, isObject, isRule, isSizeRule, make, mergeDeep, notRegex, plural, regex, register, registerImplicit, registerValueInspector, requiredIf, ruleIn, ruleNotIn, runWithValidatorContext, sameType, toDate, toSnakeCase, usePlugin, useValidatorContext };
3706
+ export { BaseRule, BaseValidator, ErrorBag, ExtendedRules, IDatabaseDriver, ImplicitRule, Lang, MessageBag, Password, ValidationException, ValidationRule, ValidationServiceProvider, Validator, addImplicitRule, buildValidationMethodName, compare, convertValuesToBoolean, convertValuesToNull, convertValuesToNumber, deepEqual, deepFind, deepFindMessage, deepSet, definePlugin, dispatchValidationErrorHooks, dispatchValidationSuccessHooks, dotify, getFormattedAttribute, getKeyCombinations, getMessage, getNumericRules, getSize, getValidationMessageType, getValidationSize, getValidationValueInspector, getValidatorContext, isArrayOfRules, isImplicitRule, isInteger, isNumericRule, isObject, isRule, isSizeRule, make, mergeDeep, notRegex, plural, regex, register, registerImplicit, registerValidationErrorHook, registerValidationSuccessHook, registerValueInspector, requiredIf, ruleIn, ruleNotIn, runWithValidatorContext, sameType, toDate, toSnakeCase, usePlugin, useValidatorContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kanun",
3
- "version": "1.1.2",
3
+ "version": "1.2.0",
4
4
  "description": "Framework-agnostic TypeScript-first validation library.",
5
5
  "type": "module",
6
6
  "files": [