Skip to content

Commit

Permalink
patch(n4s): improve types for rule chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 413c440 commit 1dddaf4
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 27 deletions.
2 changes: 1 addition & 1 deletion packages/n4s/src/compounds/__tests__/optional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('enforce.optional', () => {
expect(enforce.optional(enforce.isNumber()).run(null)).toEqual({
pass: true,
});
expect(enforce.optional(enforce.isArray()).run()).toEqual({
expect(enforce.optional(enforce.isArray()).run(undefined)).toEqual({
pass: true,
});

Expand Down
5 changes: 3 additions & 2 deletions packages/n4s/src/compounds/allOf.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import mapFirst from 'mapFirst';

import type { TLazy } from 'genEnforceLazy';
import * as ruleReturn from 'ruleReturn';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TRuleDetailedResult } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export default function allOf(
value: unknown,
...rules: TLazyRuleMethods[]
...rules: TLazy[]
): TRuleDetailedResult {
return (
mapFirst(rules, (rule, breakout) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/n4s/src/compounds/anyOf.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import mapFirst from 'mapFirst';

import type { TLazy } from 'genEnforceLazy';
import * as ruleReturn from 'ruleReturn';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TRuleDetailedResult } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export default function anyOf(
value: unknown,
...rules: TLazyRuleMethods[]
...rules: TLazy[]
): TRuleDetailedResult {
return (
mapFirst(rules, (rule, breakout) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/n4s/src/compounds/noneOf.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import mapFirst from 'mapFirst';

import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TLazy } from 'genEnforceLazy';
import type { TRuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export default function noneOf(
value: unknown,
...rules: TLazyRuleMethods[]
...rules: TLazy[]
): TRuleDetailedResult {
return (
mapFirst(rules, (rule, breakout) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/n4s/src/compounds/oneOf.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { TLazy } from 'genEnforceLazy';
import { lengthEquals } from 'lengthEquals';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TRuleDetailedResult } from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export default function oneOf(
value: unknown,
...rules: TLazyRuleMethods[]
...rules: TLazy[]
): TRuleDetailedResult {
const passing: TRuleDetailedResult[] = [];

Expand Down
5 changes: 3 additions & 2 deletions packages/n4s/src/compounds/optional.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import type { TLazy } from 'genEnforceLazy';
import { isNull } from 'isNull';
import { isUndefined } from 'isUndefined';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TRuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export default function optional(
value: any,
ruleChain: TLazyRuleMethods
ruleChain: TLazy
): TRuleDetailedResult {
if (isUndefined(value) || isNull(value)) {
return ruleReturn.passing();
Expand Down
5 changes: 3 additions & 2 deletions packages/n4s/src/lib/runLazyRule.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TLazy } from 'genEnforceLazy';
import type { TRuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';

export default function runLazyRule(
lazyRule: TLazyRuleMethods,
lazyRule: TLazy,
currentValue: any
): TRuleDetailedResult {
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/n4s/src/runtime/__tests__/enforceContext.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import enforce from 'enforce';

const keepContext = jest.fn();
let keepContext = jest.fn();

describe('enforce.context', () => {
beforeEach(() => {
Expand Down Expand Up @@ -207,7 +207,7 @@ enforce.extend({
isFriendTheSameAsUser: value => {
const context = enforce.context();

if (value === context.parent().parent().value.username) {
if (value === context?.parent()?.parent()?.value.username) {
return { pass: false };
}

Expand Down
4 changes: 2 additions & 2 deletions packages/n4s/src/runtime/enforce.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assign from 'assign';

import eachEnforceRule from 'eachEnforceRule';
import { ctx, CTXType } from 'enforceContext';
import { ctx, TEnforceContext } from 'enforceContext';
import enforceEager from 'enforceEager';
import genEnforceLazy, { TLazyRules } from 'genEnforceLazy';
import isProxySupported from 'isProxySupported';
Expand Down Expand Up @@ -77,7 +77,7 @@ const enforce = genEnforce();

export default enforce;

export type TEnforce = { context: () => CTXType } & Record<
export type TEnforce = { context: () => TEnforceContext } & Record<
string,
(...args: TArgs) => TLazyRuleMethods & TLazyRules
> &
Expand Down
20 changes: 19 additions & 1 deletion packages/n4s/src/runtime/enforceContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,38 @@ export const ctx = createContext<CTXType>((ctxRef, parentContext): CTXType => {
return {
meta: ctxRef.meta || {},
value: ctxRef.value,
parent: (): null | CTXType => parentContext,
parent: (): TEnforceContext => stripContext(parentContext),
};
}

return parentContext;
});

function stripContext(ctx: null | CTXType): TEnforceContext {
if (!ctx) {
return ctx;
}

return {
value: ctx.value,
meta: ctx.meta,
parent: ctx.parent,
};
}

export type CTXType = {
meta: Record<string, any>;
value: any;
set?: boolean;
parent: () => CTXType | null;
};

export type TEnforceContext = null | {
meta: Record<string, any>;
value: any;
parent: () => TEnforceContext;
};

function emptyParent(): null {
return null;
}
8 changes: 5 additions & 3 deletions packages/n4s/src/runtime/genEnforceLazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ export default function genEnforceLazy(key: string) {
}
}

export type TLazyRules = Record<string, (...args: TArgs) => TLazyRules> &
export type TLazyRules = Record<string, (...args: TArgs) => TLazy> &
{
[P in keyof TCompounds]: (
...args: DropFirst<Parameters<TCompounds[P]>> | TArgs
) => TLazyRules & TLazyRuleMethods;
) => TLazy;
} &
{
[P in TBaseRules]: (
...args: DropFirst<Parameters<typeof baseRules[P]>> | TArgs
) => TLazyRules & TLazyRuleMethods;
) => TLazy;
};

export type TLazy = TLazyRules & TLazyRuleMethods;
5 changes: 3 additions & 2 deletions packages/n4s/src/schema/isArrayOf.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import mapFirst from 'mapFirst';

import { ctx } from 'enforceContext';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TLazy } from 'genEnforceLazy';
import type { TRuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export default function isArrayOf(
inputArray: any[],
currentRule: TLazyRuleMethods
currentRule: TLazy
): TRuleDetailedResult {
return (
mapFirst(inputArray, (currentValue, breakout, index) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/n4s/src/schema/loose.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ctx } from 'enforceContext';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TLazy } from 'genEnforceLazy';
import type { TRuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';
import runLazyRule from 'runLazyRule';

export default function loose(
inputObject: Record<string, any>,
shapeObject: Record<string, TLazyRuleMethods>
shapeObject: Record<string, TLazy>
): TRuleDetailedResult {
for (const key in shapeObject) {
const currentValue = inputObject[key];
Expand Down
5 changes: 3 additions & 2 deletions packages/n4s/src/schema/shape.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import hasOwnProperty from 'hasOwnProperty';

import type { TLazy } from 'genEnforceLazy';
import loose from 'loose';
import type { TRuleDetailedResult, TLazyRuleMethods } from 'ruleReturn';
import type { TRuleDetailedResult } from 'ruleReturn';
import * as ruleReturn from 'ruleReturn';

export default function shape(
inputObject: Record<string, any>,
shapeObject: Record<string, TLazyRuleMethods>
shapeObject: Record<string, TLazy>
): TRuleDetailedResult {
const baseRes = loose(inputObject, shapeObject);
if (!baseRes.pass) {
Expand Down

0 comments on commit 1dddaf4

Please sign in to comment.