Skip to content

Commit

Permalink
patch(n4s): Show boolean passing status in enforce result
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Dec 27, 2023
1 parent d664f5d commit 2323b74
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/n4s/src/runtime/enforceEager.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<string> = undefined;

Expand All @@ -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
Expand All @@ -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;
};
}
Expand Down

0 comments on commit 2323b74

Please sign in to comment.