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.cjs CHANGED
@@ -1699,13 +1699,17 @@ function registerImplicit(rule, validate, replaceMessage) {
1699
1699
  //#endregion
1700
1700
  //#region src/Plugin.ts
1701
1701
  const valueInspectors = [];
1702
+ const validationErrorHooks = [];
1703
+ const validationSuccessHooks = [];
1702
1704
  const api = {
1703
1705
  registerRule: register,
1704
1706
  registerImplicitRule: registerImplicit,
1705
1707
  registerValueInspector,
1706
1708
  extendTranslations: (translations) => {
1707
1709
  Lang.extendTranslationObject(translations);
1708
- }
1710
+ },
1711
+ onValidationError: registerValidationErrorHook,
1712
+ onValidationSuccess: registerValidationSuccessHook
1709
1713
  };
1710
1714
  function definePlugin(plugin) {
1711
1715
  return plugin;
@@ -1721,6 +1725,18 @@ function registerValueInspector(inspector) {
1721
1725
  }
1722
1726
  valueInspectors.push(inspector);
1723
1727
  }
1728
+ function registerValidationErrorHook(hook) {
1729
+ validationErrorHooks.push(hook);
1730
+ }
1731
+ function registerValidationSuccessHook(hook) {
1732
+ validationSuccessHooks.push(hook);
1733
+ }
1734
+ async function dispatchValidationErrorHooks(validator) {
1735
+ for (const hook of validationErrorHooks) await hook(validator);
1736
+ }
1737
+ async function dispatchValidationSuccessHooks(validator) {
1738
+ for (const hook of validationSuccessHooks) await hook(validator);
1739
+ }
1724
1740
  function getValidationValueInspector(value) {
1725
1741
  return valueInspectors.find((inspector) => inspector.matches(value));
1726
1742
  }
@@ -3417,6 +3433,8 @@ var Validator = class Validator {
3417
3433
  if (this.executed) return this._errors.isEmpty();
3418
3434
  const exec = await this.execute();
3419
3435
  for (const after of this.#after) after();
3436
+ if (exec.passing) await dispatchValidationSuccessHooks(this);
3437
+ else await dispatchValidationErrorHooks(this);
3420
3438
  return exec.passing;
3421
3439
  }
3422
3440
  /**
@@ -3734,6 +3752,8 @@ exports.deepFind = deepFind;
3734
3752
  exports.deepFindMessage = deepFindMessage;
3735
3753
  exports.deepSet = deepSet;
3736
3754
  exports.definePlugin = definePlugin;
3755
+ exports.dispatchValidationErrorHooks = dispatchValidationErrorHooks;
3756
+ exports.dispatchValidationSuccessHooks = dispatchValidationSuccessHooks;
3737
3757
  exports.dotify = dotify;
3738
3758
  exports.getFormattedAttribute = getFormattedAttribute;
3739
3759
  exports.getKeyCombinations = getKeyCombinations;
@@ -3758,6 +3778,8 @@ exports.plural = plural;
3758
3778
  exports.regex = regex;
3759
3779
  exports.register = register;
3760
3780
  exports.registerImplicit = registerImplicit;
3781
+ exports.registerValidationErrorHook = registerValidationErrorHook;
3782
+ exports.registerValidationSuccessHook = registerValidationSuccessHook;
3761
3783
  exports.registerValueInspector = registerValueInspector;
3762
3784
  exports.requiredIf = requiredIf;
3763
3785
  exports.ruleIn = ruleIn;