From 2323b74327ac1513612ac13f4e0f9f5539239166 Mon Sep 17 00:00:00 2001 From: Evyatar Date: Thu, 28 Dec 2023 00:52:53 +0200 Subject: [PATCH] patch(n4s): Show boolean passing status in enforce result --- packages/n4s/src/runtime/enforceEager.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/n4s/src/runtime/enforceEager.ts b/packages/n4s/src/runtime/enforceEager.ts index 31e635eeb..c9dd4e668 100644 --- a/packages/n4s/src/runtime/enforceEager.ts +++ b/packages/n4s/src/runtime/enforceEager.ts @@ -1,6 +1,7 @@ import { invariant, StringObject, isNullish, Maybe } from 'vest-utils'; import { ctx } from 'enforceContext'; +import { RuleDetailedResult } from 'ruleReturn'; import { getRule, RuleValue, Args, RuleBase } from 'runtimeRules'; import { transformResult } from 'transformResult'; @@ -9,12 +10,16 @@ type TModifiers = { message: (input: string) => EnforceEagerReturn; }; -type EnforceEagerReturn = IRules & TModifiers; +type EnforceEagerReturn = IRules & + TModifiers & { + pass: boolean; + }; // eslint-disable-next-line max-lines-per-function export default function enforceEager(value: RuleValue): EnforceEagerReturn { const target = { message, + pass: false, } as EnforceEagerReturn; let customMessage: Maybe = undefined; @@ -40,9 +45,9 @@ export default function enforceEager(value: RuleValue): EnforceEagerReturn { function genRuleCall( target: EnforceEagerReturn, rule: RuleBase, - ruleName: string + ruleName: string, ) { - return function ruleCall(...args: Args) { + return function ruleCall(...args: Args): EnforceEagerReturn { // Order of operation: // 1. Create a context with the value being enforced // 2. Call the rule within the context, and pass over the arguments passed to it @@ -63,6 +68,12 @@ export default function enforceEager(value: RuleValue): EnforceEagerReturn { // or throw a string value if the rule has a message defined in it. invariant(transformedResult.pass, enforceMessage()); + // This is not really needed because it will always be true + // As we're throwing an error on failure + // but it is here so that users have a sense of what is happening + // when they try to log the result of enforce and not just see a proxy object + target.pass = transformedResult.pass; + return target; }; }