n4s 2.2.0-rc.1 → 4.0.0-dev-e266d9
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/CHANGELOG.md +129 -0
- package/LICENSE +1 -2
- package/README.md +10 -4
- package/compose/package.json +7 -0
- package/compounds/package.json +7 -0
- package/dist/cjs/compose.development.js +139 -0
- package/dist/cjs/compose.js +7 -0
- package/dist/cjs/compose.production.js +1 -0
- package/dist/cjs/compounds.development.js +132 -0
- package/dist/cjs/compounds.js +7 -0
- package/dist/cjs/compounds.production.js +1 -0
- package/dist/cjs/n4s.development.js +602 -0
- package/dist/cjs/n4s.js +7 -0
- package/dist/cjs/n4s.production.js +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/schema.development.js +144 -0
- package/dist/cjs/schema.js +7 -0
- package/dist/cjs/schema.production.js +1 -0
- package/dist/es/compose.development.js +137 -0
- package/dist/es/compose.production.js +1 -0
- package/dist/es/compounds.development.js +130 -0
- package/dist/es/compounds.production.js +1 -0
- package/dist/es/n4s.development.js +597 -0
- package/dist/es/n4s.production.js +1 -0
- package/dist/es/package.json +1 -0
- package/dist/es/schema.development.js +140 -0
- package/dist/es/schema.production.js +1 -0
- package/dist/umd/compose.development.js +143 -0
- package/dist/umd/compose.production.js +1 -0
- package/dist/umd/compounds.development.js +136 -0
- package/dist/umd/compounds.production.js +1 -0
- package/dist/umd/n4s.development.js +606 -0
- package/dist/umd/n4s.production.js +1 -0
- package/dist/umd/schema.development.js +148 -0
- package/dist/umd/schema.production.js +1 -0
- package/docs/README.md +10 -4
- package/docs/_sidebar.md +2 -2
- package/docs/external.md +27 -0
- package/docs/rules.md +74 -0
- package/package.json +126 -67
- package/schema/package.json +7 -0
- package/tsconfig.json +8 -0
- package/types/compose.d.ts +134 -0
- package/types/compounds.d.ts +146 -0
- package/types/n4s.d.ts +167 -0
- package/types/schema.d.ts +151 -0
- package/config/rollup/enforce.js +0 -13
- package/config/rollup/enforceExtended.js +0 -10
- package/config/rollup/ensure.js +0 -10
- package/config/rollup/rollup.config.js +0 -5
- package/docs/business_rules.md +0 -80
- package/docs/custom.md +0 -54
- package/docs/ensure.md +0 -40
- package/enforceExtended.cjs.development.js +0 -1949
- package/enforceExtended.cjs.production.js +0 -1949
- package/enforceExtended.cjs.production.min.js +0 -1
- package/enforceExtended.umd.development.js +0 -1955
- package/enforceExtended.umd.production.js +0 -1972
- package/enforceExtended.umd.production.min.js +0 -1
- package/ensure.cjs.development.js +0 -418
- package/ensure.cjs.production.js +0 -418
- package/ensure.cjs.production.min.js +0 -1
- package/ensure.umd.development.js +0 -424
- package/ensure.umd.production.js +0 -444
- package/ensure.umd.production.min.js +0 -1
- package/esm/enforceExtended.mjs.development.js +0 -1947
- package/esm/enforceExtended.mjs.production.js +0 -1947
- package/esm/enforceExtended.mjs.production.min.js +0 -1
- package/esm/ensure.mjs.development.js +0 -416
- package/esm/ensure.mjs.production.js +0 -416
- package/esm/ensure.mjs.production.min.js +0 -1
- package/esm/n4s.mjs.development.js +0 -394
- package/esm/n4s.mjs.production.js +0 -394
- package/esm/n4s.mjs.production.min.js +0 -1
- package/esm/package.json +0 -1
- package/jest.config.js +0 -3
- package/n4s.cjs.development.js +0 -396
- package/n4s.cjs.production.js +0 -396
- package/n4s.cjs.production.min.js +0 -1
- package/n4s.umd.development.js +0 -402
- package/n4s.umd.index.js +0 -7
- package/n4s.umd.production.js +0 -419
- package/n4s.umd.production.min.js +0 -1
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
type DropFirst<T extends unknown[]> = T extends [
|
|
2
|
+
unknown,
|
|
3
|
+
...infer U
|
|
4
|
+
] ? U : never;
|
|
5
|
+
type TStringable = string | ((...args: any[]) => string);
|
|
6
|
+
type TRuleReturn = boolean | {
|
|
7
|
+
pass: boolean;
|
|
8
|
+
message?: TStringable;
|
|
9
|
+
};
|
|
10
|
+
type TRuleDetailedResult = {
|
|
11
|
+
pass: boolean;
|
|
12
|
+
message?: string;
|
|
13
|
+
};
|
|
14
|
+
type TArgs = any[];
|
|
15
|
+
type TBaseRules = typeof baseRules;
|
|
16
|
+
type KBaseRules = keyof TBaseRules;
|
|
17
|
+
declare function condition(value: any, callback: (value: any) => TRuleReturn): TRuleReturn;
|
|
18
|
+
declare function endsWith(value: string, arg1: string): boolean;
|
|
19
|
+
declare function equals(value: unknown, arg1: unknown): boolean;
|
|
20
|
+
declare function greaterThan(value: number | string, gt: number | string): boolean;
|
|
21
|
+
declare function greaterThanOrEquals(value: string | number, gte: string | number): boolean;
|
|
22
|
+
declare function inside(value: unknown, arg1: string | unknown[]): boolean;
|
|
23
|
+
// The module is named "isArrayValue" since it
|
|
24
|
+
// is conflicting with a nested npm dependency.
|
|
25
|
+
// We may need to revisit this in the future.
|
|
26
|
+
declare function isArray(value: unknown): value is Array<unknown>;
|
|
27
|
+
declare function isBetween(value: number | string, min: number | string, max: number | string): boolean;
|
|
28
|
+
declare function isBlank(value: unknown): boolean;
|
|
29
|
+
declare function isBoolean(value: unknown): value is boolean;
|
|
30
|
+
declare function isEmpty(value: unknown): boolean;
|
|
31
|
+
declare function isNaN(value: unknown): boolean;
|
|
32
|
+
declare function isNegative(value: number | string): boolean;
|
|
33
|
+
declare function isNull(value: unknown): value is null;
|
|
34
|
+
declare function isNullish(value: any): value is null | undefined;
|
|
35
|
+
declare function isNumber(value: unknown): value is number;
|
|
36
|
+
declare function isNumeric(value: string | number): boolean;
|
|
37
|
+
declare function isStringValue(v: unknown): v is string;
|
|
38
|
+
declare function isTruthy(value: unknown): boolean;
|
|
39
|
+
declare function isUndefined(value?: unknown): boolean;
|
|
40
|
+
declare function lengthEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
41
|
+
declare function lessThan(value: string | number, lt: string | number): boolean;
|
|
42
|
+
declare function lessThanOrEquals(value: string | number, lte: string | number): boolean;
|
|
43
|
+
declare function longerThan(value: string | unknown[], arg1: string | number): boolean;
|
|
44
|
+
declare function longerThanOrEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
45
|
+
declare function matches(value: string, regex: RegExp | string): boolean;
|
|
46
|
+
declare function numberEquals(value: string | number, eq: string | number): boolean;
|
|
47
|
+
declare function shorterThan(value: string | unknown[], arg1: string | number): boolean;
|
|
48
|
+
declare function shorterThanOrEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
49
|
+
declare function startsWith(value: string, arg1: string): boolean;
|
|
50
|
+
declare const baseRules: {
|
|
51
|
+
condition: typeof condition;
|
|
52
|
+
doesNotEndWith: (value: string, arg1: string) => boolean;
|
|
53
|
+
doesNotStartWith: (value: string, arg1: string) => boolean;
|
|
54
|
+
endsWith: typeof endsWith;
|
|
55
|
+
equals: typeof equals;
|
|
56
|
+
greaterThan: typeof greaterThan;
|
|
57
|
+
greaterThanOrEquals: typeof greaterThanOrEquals;
|
|
58
|
+
gt: typeof greaterThan;
|
|
59
|
+
gte: typeof greaterThanOrEquals;
|
|
60
|
+
inside: typeof inside;
|
|
61
|
+
isArray: typeof isArray;
|
|
62
|
+
isBetween: typeof isBetween;
|
|
63
|
+
isBlank: typeof isBlank;
|
|
64
|
+
isBoolean: typeof isBoolean;
|
|
65
|
+
isEmpty: typeof isEmpty;
|
|
66
|
+
isEven: (value: any) => boolean;
|
|
67
|
+
isFalsy: (value: unknown) => boolean;
|
|
68
|
+
isNaN: typeof isNaN;
|
|
69
|
+
isNegative: typeof isNegative;
|
|
70
|
+
isNotArray: (value: unknown) => boolean;
|
|
71
|
+
isNotBetween: (value: string | number, min: string | number, max: string | number) => boolean;
|
|
72
|
+
isNotBlank: (value: unknown) => boolean;
|
|
73
|
+
isNotBoolean: (value: unknown) => boolean;
|
|
74
|
+
isNotEmpty: (value: unknown) => boolean;
|
|
75
|
+
isNotNaN: (value: unknown) => boolean;
|
|
76
|
+
isNotNull: (value: unknown) => boolean;
|
|
77
|
+
isNotNullish: (value: any) => boolean;
|
|
78
|
+
isNotNumber: (value: unknown) => boolean;
|
|
79
|
+
isNotNumeric: (value: string | number) => boolean;
|
|
80
|
+
isNotString: (v: unknown) => boolean;
|
|
81
|
+
isNotUndefined: (value?: unknown) => boolean;
|
|
82
|
+
isNull: typeof isNull;
|
|
83
|
+
isNullish: typeof isNullish;
|
|
84
|
+
isNumber: typeof isNumber;
|
|
85
|
+
isNumeric: typeof isNumeric;
|
|
86
|
+
isOdd: (value: any) => boolean;
|
|
87
|
+
isPositive: (value: string | number) => boolean;
|
|
88
|
+
isString: typeof isStringValue;
|
|
89
|
+
isTruthy: typeof isTruthy;
|
|
90
|
+
isUndefined: typeof isUndefined;
|
|
91
|
+
lengthEquals: typeof lengthEquals;
|
|
92
|
+
lengthNotEquals: (value: string | unknown[], arg1: string | number) => boolean;
|
|
93
|
+
lessThan: typeof lessThan;
|
|
94
|
+
lessThanOrEquals: typeof lessThanOrEquals;
|
|
95
|
+
longerThan: typeof longerThan;
|
|
96
|
+
longerThanOrEquals: typeof longerThanOrEquals;
|
|
97
|
+
lt: typeof lessThan;
|
|
98
|
+
lte: typeof lessThanOrEquals;
|
|
99
|
+
matches: typeof matches;
|
|
100
|
+
notEquals: (value: unknown, arg1: unknown) => boolean;
|
|
101
|
+
notInside: (value: unknown, arg1: string | unknown[]) => boolean;
|
|
102
|
+
notMatches: (value: string, regex: string | RegExp) => boolean;
|
|
103
|
+
numberEquals: typeof numberEquals;
|
|
104
|
+
numberNotEquals: (value: string | number, eq: string | number) => boolean;
|
|
105
|
+
shorterThan: typeof shorterThan;
|
|
106
|
+
shorterThanOrEquals: typeof shorterThanOrEquals;
|
|
107
|
+
startsWith: typeof startsWith;
|
|
108
|
+
};
|
|
109
|
+
type TRules<E = Record<string, unknown>> = n4s.EnforceCustomMatchers<TRules<E> & E> & Record<string, (...args: TArgs) => TRules<E> & E> & {
|
|
110
|
+
[P in KBaseRules]: (...args: DropFirst<Parameters<TBaseRules[P]>> | TArgs) => TRules<E> & E;
|
|
111
|
+
};
|
|
112
|
+
/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-empty-interface */
|
|
113
|
+
declare global {
|
|
114
|
+
namespace n4s {
|
|
115
|
+
interface IRules<E> extends TRules<E> {
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
type TLazyRules = n4s.IRules<TLazyRuleMethods>;
|
|
120
|
+
type TLazy = TLazyRules & TLazyRuleMethods &
|
|
121
|
+
// This is a "catch all" hack to make TS happy while not
|
|
122
|
+
// losing type hints
|
|
123
|
+
Record<string, (...args: any[]) => any>;
|
|
124
|
+
type TLazyRuleMethods = TLazyRuleRunners & {
|
|
125
|
+
message: (message: TLazyMessage) => TLazy;
|
|
126
|
+
};
|
|
127
|
+
type TLazyRuleRunners = {
|
|
128
|
+
test: (value: unknown) => boolean;
|
|
129
|
+
run: (value: unknown) => TRuleDetailedResult;
|
|
130
|
+
};
|
|
131
|
+
type TLazyMessage = string | ((value: unknown, originalMessage?: TStringable) => string);
|
|
132
|
+
declare function allOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult;
|
|
133
|
+
declare function anyOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult;
|
|
134
|
+
declare function noneOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult;
|
|
135
|
+
declare function oneOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult;
|
|
136
|
+
declare global {
|
|
137
|
+
namespace n4s {
|
|
138
|
+
interface EnforceCustomMatchers<R> {
|
|
139
|
+
allOf: (...args: DropFirst<Parameters<typeof allOf>>) => R;
|
|
140
|
+
anyOf: (...args: DropFirst<Parameters<typeof anyOf>>) => R;
|
|
141
|
+
noneOf: (...args: DropFirst<Parameters<typeof noneOf>>) => R;
|
|
142
|
+
oneOf: (...args: DropFirst<Parameters<typeof oneOf>>) => R;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export {};
|
package/types/n4s.d.ts
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
declare const ctx: {
|
|
2
|
+
run: <R>(ctxRef: Partial<CTXType>, fn: (context: CTXType) => R) => R;
|
|
3
|
+
bind: <Fn extends (...args: any[]) => any>(ctxRef: Partial<CTXType>, fn: Fn) => Fn;
|
|
4
|
+
use: () => CTXType | undefined;
|
|
5
|
+
useX: (errorMessage?: string | undefined) => CTXType;
|
|
6
|
+
};
|
|
7
|
+
type CTXType = {
|
|
8
|
+
meta: Record<string, any>;
|
|
9
|
+
value: any;
|
|
10
|
+
set?: boolean;
|
|
11
|
+
parent: () => CTXType | null;
|
|
12
|
+
};
|
|
13
|
+
type TEnforceContext = null | {
|
|
14
|
+
meta: Record<string, any>;
|
|
15
|
+
value: any;
|
|
16
|
+
parent: () => TEnforceContext;
|
|
17
|
+
};
|
|
18
|
+
type DropFirst<T extends unknown[]> = T extends [
|
|
19
|
+
unknown,
|
|
20
|
+
...infer U
|
|
21
|
+
] ? U : never;
|
|
22
|
+
type TStringable = string | ((...args: any[]) => string);
|
|
23
|
+
type TRuleReturn = boolean | {
|
|
24
|
+
pass: boolean;
|
|
25
|
+
message?: TStringable;
|
|
26
|
+
};
|
|
27
|
+
type TRuleDetailedResult = {
|
|
28
|
+
pass: boolean;
|
|
29
|
+
message?: string;
|
|
30
|
+
};
|
|
31
|
+
type TArgs = any[];
|
|
32
|
+
type TRuleValue = any;
|
|
33
|
+
type TRuleBase = (value: TRuleValue, ...args: TArgs) => TRuleReturn;
|
|
34
|
+
type TRule = Record<string, TRuleBase>;
|
|
35
|
+
type TBaseRules = typeof baseRules;
|
|
36
|
+
type KBaseRules = keyof TBaseRules;
|
|
37
|
+
declare function condition(value: any, callback: (value: any) => TRuleReturn): TRuleReturn;
|
|
38
|
+
declare function endsWith(value: string, arg1: string): boolean;
|
|
39
|
+
declare function equals(value: unknown, arg1: unknown): boolean;
|
|
40
|
+
declare function greaterThan(value: number | string, gt: number | string): boolean;
|
|
41
|
+
declare function greaterThanOrEquals(value: string | number, gte: string | number): boolean;
|
|
42
|
+
declare function inside(value: unknown, arg1: string | unknown[]): boolean;
|
|
43
|
+
// The module is named "isArrayValue" since it
|
|
44
|
+
// is conflicting with a nested npm dependency.
|
|
45
|
+
// We may need to revisit this in the future.
|
|
46
|
+
declare function isArray(value: unknown): value is Array<unknown>;
|
|
47
|
+
declare function isBetween(value: number | string, min: number | string, max: number | string): boolean;
|
|
48
|
+
declare function isBlank(value: unknown): boolean;
|
|
49
|
+
declare function isBoolean(value: unknown): value is boolean;
|
|
50
|
+
declare function isEmpty(value: unknown): boolean;
|
|
51
|
+
declare function isNaN(value: unknown): boolean;
|
|
52
|
+
declare function isNegative(value: number | string): boolean;
|
|
53
|
+
declare function isNull(value: unknown): value is null;
|
|
54
|
+
declare function isNullish(value: any): value is null | undefined;
|
|
55
|
+
declare function isNumber(value: unknown): value is number;
|
|
56
|
+
declare function isNumeric(value: string | number): boolean;
|
|
57
|
+
declare function isStringValue(v: unknown): v is string;
|
|
58
|
+
declare function isTruthy(value: unknown): boolean;
|
|
59
|
+
declare function isUndefined(value?: unknown): boolean;
|
|
60
|
+
declare function lengthEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
61
|
+
declare function lessThan(value: string | number, lt: string | number): boolean;
|
|
62
|
+
declare function lessThanOrEquals(value: string | number, lte: string | number): boolean;
|
|
63
|
+
declare function longerThan(value: string | unknown[], arg1: string | number): boolean;
|
|
64
|
+
declare function longerThanOrEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
65
|
+
declare function matches(value: string, regex: RegExp | string): boolean;
|
|
66
|
+
declare function numberEquals(value: string | number, eq: string | number): boolean;
|
|
67
|
+
declare function shorterThan(value: string | unknown[], arg1: string | number): boolean;
|
|
68
|
+
declare function shorterThanOrEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
69
|
+
declare function startsWith(value: string, arg1: string): boolean;
|
|
70
|
+
declare const baseRules: {
|
|
71
|
+
condition: typeof condition;
|
|
72
|
+
doesNotEndWith: (value: string, arg1: string) => boolean;
|
|
73
|
+
doesNotStartWith: (value: string, arg1: string) => boolean;
|
|
74
|
+
endsWith: typeof endsWith;
|
|
75
|
+
equals: typeof equals;
|
|
76
|
+
greaterThan: typeof greaterThan;
|
|
77
|
+
greaterThanOrEquals: typeof greaterThanOrEquals;
|
|
78
|
+
gt: typeof greaterThan;
|
|
79
|
+
gte: typeof greaterThanOrEquals;
|
|
80
|
+
inside: typeof inside;
|
|
81
|
+
isArray: typeof isArray;
|
|
82
|
+
isBetween: typeof isBetween;
|
|
83
|
+
isBlank: typeof isBlank;
|
|
84
|
+
isBoolean: typeof isBoolean;
|
|
85
|
+
isEmpty: typeof isEmpty;
|
|
86
|
+
isEven: (value: any) => boolean;
|
|
87
|
+
isFalsy: (value: unknown) => boolean;
|
|
88
|
+
isNaN: typeof isNaN;
|
|
89
|
+
isNegative: typeof isNegative;
|
|
90
|
+
isNotArray: (value: unknown) => boolean;
|
|
91
|
+
isNotBetween: (value: string | number, min: string | number, max: string | number) => boolean;
|
|
92
|
+
isNotBlank: (value: unknown) => boolean;
|
|
93
|
+
isNotBoolean: (value: unknown) => boolean;
|
|
94
|
+
isNotEmpty: (value: unknown) => boolean;
|
|
95
|
+
isNotNaN: (value: unknown) => boolean;
|
|
96
|
+
isNotNull: (value: unknown) => boolean;
|
|
97
|
+
isNotNullish: (value: any) => boolean;
|
|
98
|
+
isNotNumber: (value: unknown) => boolean;
|
|
99
|
+
isNotNumeric: (value: string | number) => boolean;
|
|
100
|
+
isNotString: (v: unknown) => boolean;
|
|
101
|
+
isNotUndefined: (value?: unknown) => boolean;
|
|
102
|
+
isNull: typeof isNull;
|
|
103
|
+
isNullish: typeof isNullish;
|
|
104
|
+
isNumber: typeof isNumber;
|
|
105
|
+
isNumeric: typeof isNumeric;
|
|
106
|
+
isOdd: (value: any) => boolean;
|
|
107
|
+
isPositive: (value: string | number) => boolean;
|
|
108
|
+
isString: typeof isStringValue;
|
|
109
|
+
isTruthy: typeof isTruthy;
|
|
110
|
+
isUndefined: typeof isUndefined;
|
|
111
|
+
lengthEquals: typeof lengthEquals;
|
|
112
|
+
lengthNotEquals: (value: string | unknown[], arg1: string | number) => boolean;
|
|
113
|
+
lessThan: typeof lessThan;
|
|
114
|
+
lessThanOrEquals: typeof lessThanOrEquals;
|
|
115
|
+
longerThan: typeof longerThan;
|
|
116
|
+
longerThanOrEquals: typeof longerThanOrEquals;
|
|
117
|
+
lt: typeof lessThan;
|
|
118
|
+
lte: typeof lessThanOrEquals;
|
|
119
|
+
matches: typeof matches;
|
|
120
|
+
notEquals: (value: unknown, arg1: unknown) => boolean;
|
|
121
|
+
notInside: (value: unknown, arg1: string | unknown[]) => boolean;
|
|
122
|
+
notMatches: (value: string, regex: string | RegExp) => boolean;
|
|
123
|
+
numberEquals: typeof numberEquals;
|
|
124
|
+
numberNotEquals: (value: string | number, eq: string | number) => boolean;
|
|
125
|
+
shorterThan: typeof shorterThan;
|
|
126
|
+
shorterThanOrEquals: typeof shorterThanOrEquals;
|
|
127
|
+
startsWith: typeof startsWith;
|
|
128
|
+
};
|
|
129
|
+
type TRules<E = Record<string, unknown>> = n4s.EnforceCustomMatchers<TRules<E> & E> & Record<string, (...args: TArgs) => TRules<E> & E> & {
|
|
130
|
+
[P in KBaseRules]: (...args: DropFirst<Parameters<TBaseRules[P]>> | TArgs) => TRules<E> & E;
|
|
131
|
+
};
|
|
132
|
+
/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-empty-interface */
|
|
133
|
+
declare global {
|
|
134
|
+
namespace n4s {
|
|
135
|
+
interface IRules<E> extends TRules<E> {
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
type IRules = n4s.IRules<Record<string, any>>;
|
|
140
|
+
declare function enforceEager(value: TRuleValue): IRules;
|
|
141
|
+
type TEnforceEager = typeof enforceEager;
|
|
142
|
+
type TLazyRules = n4s.IRules<TLazyRuleMethods>;
|
|
143
|
+
type TLazy = TLazyRules & TLazyRuleMethods &
|
|
144
|
+
// This is a "catch all" hack to make TS happy while not
|
|
145
|
+
// losing type hints
|
|
146
|
+
Record<string, (...args: any[]) => any>;
|
|
147
|
+
type TLazyRuleMethods = TLazyRuleRunners & {
|
|
148
|
+
message: (message: TLazyMessage) => TLazy;
|
|
149
|
+
};
|
|
150
|
+
type TLazyRuleRunners = {
|
|
151
|
+
test: (value: unknown) => boolean;
|
|
152
|
+
run: (value: unknown) => TRuleDetailedResult;
|
|
153
|
+
};
|
|
154
|
+
type TLazyMessage = string | ((value: unknown, originalMessage?: TStringable) => string);
|
|
155
|
+
declare const enforce: TEnforce;
|
|
156
|
+
type TEnforce = TEnforceMethods & TLazyRules & TEnforceEager;
|
|
157
|
+
type TEnforceMethods = {
|
|
158
|
+
context: () => TEnforceContext;
|
|
159
|
+
extend: (customRules: TRule) => void;
|
|
160
|
+
};
|
|
161
|
+
declare global {
|
|
162
|
+
namespace n4s {
|
|
163
|
+
interface EnforceCustomMatchers<R> {
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
export { enforce, ctx };
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
type DropFirst<T extends unknown[]> = T extends [
|
|
2
|
+
unknown,
|
|
3
|
+
...infer U
|
|
4
|
+
] ? U : never;
|
|
5
|
+
type TStringable = string | ((...args: any[]) => string);
|
|
6
|
+
type TRuleReturn = boolean | {
|
|
7
|
+
pass: boolean;
|
|
8
|
+
message?: TStringable;
|
|
9
|
+
};
|
|
10
|
+
type TRuleDetailedResult = {
|
|
11
|
+
pass: boolean;
|
|
12
|
+
message?: string;
|
|
13
|
+
};
|
|
14
|
+
type TArgs = any[];
|
|
15
|
+
type TBaseRules = typeof baseRules;
|
|
16
|
+
type KBaseRules = keyof TBaseRules;
|
|
17
|
+
declare function condition(value: any, callback: (value: any) => TRuleReturn): TRuleReturn;
|
|
18
|
+
declare function endsWith(value: string, arg1: string): boolean;
|
|
19
|
+
declare function equals(value: unknown, arg1: unknown): boolean;
|
|
20
|
+
declare function greaterThan(value: number | string, gt: number | string): boolean;
|
|
21
|
+
declare function greaterThanOrEquals(value: string | number, gte: string | number): boolean;
|
|
22
|
+
declare function inside(value: unknown, arg1: string | unknown[]): boolean;
|
|
23
|
+
// The module is named "isArrayValue" since it
|
|
24
|
+
// is conflicting with a nested npm dependency.
|
|
25
|
+
// We may need to revisit this in the future.
|
|
26
|
+
declare function isArray(value: unknown): value is Array<unknown>;
|
|
27
|
+
declare function isBetween(value: number | string, min: number | string, max: number | string): boolean;
|
|
28
|
+
declare function isBlank(value: unknown): boolean;
|
|
29
|
+
declare function isBoolean(value: unknown): value is boolean;
|
|
30
|
+
declare function isEmpty(value: unknown): boolean;
|
|
31
|
+
declare function isNaN(value: unknown): boolean;
|
|
32
|
+
declare function isNegative(value: number | string): boolean;
|
|
33
|
+
declare function isNull(value: unknown): value is null;
|
|
34
|
+
declare function isNullish(value: any): value is null | undefined;
|
|
35
|
+
declare function isNumber(value: unknown): value is number;
|
|
36
|
+
declare function isNumeric(value: string | number): boolean;
|
|
37
|
+
declare function isStringValue(v: unknown): v is string;
|
|
38
|
+
declare function isTruthy(value: unknown): boolean;
|
|
39
|
+
declare function isUndefined(value?: unknown): boolean;
|
|
40
|
+
declare function lengthEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
41
|
+
declare function lessThan(value: string | number, lt: string | number): boolean;
|
|
42
|
+
declare function lessThanOrEquals(value: string | number, lte: string | number): boolean;
|
|
43
|
+
declare function longerThan(value: string | unknown[], arg1: string | number): boolean;
|
|
44
|
+
declare function longerThanOrEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
45
|
+
declare function matches(value: string, regex: RegExp | string): boolean;
|
|
46
|
+
declare function numberEquals(value: string | number, eq: string | number): boolean;
|
|
47
|
+
declare function shorterThan(value: string | unknown[], arg1: string | number): boolean;
|
|
48
|
+
declare function shorterThanOrEquals(value: string | unknown[], arg1: string | number): boolean;
|
|
49
|
+
declare function startsWith(value: string, arg1: string): boolean;
|
|
50
|
+
declare const baseRules: {
|
|
51
|
+
condition: typeof condition;
|
|
52
|
+
doesNotEndWith: (value: string, arg1: string) => boolean;
|
|
53
|
+
doesNotStartWith: (value: string, arg1: string) => boolean;
|
|
54
|
+
endsWith: typeof endsWith;
|
|
55
|
+
equals: typeof equals;
|
|
56
|
+
greaterThan: typeof greaterThan;
|
|
57
|
+
greaterThanOrEquals: typeof greaterThanOrEquals;
|
|
58
|
+
gt: typeof greaterThan;
|
|
59
|
+
gte: typeof greaterThanOrEquals;
|
|
60
|
+
inside: typeof inside;
|
|
61
|
+
isArray: typeof isArray;
|
|
62
|
+
isBetween: typeof isBetween;
|
|
63
|
+
isBlank: typeof isBlank;
|
|
64
|
+
isBoolean: typeof isBoolean;
|
|
65
|
+
isEmpty: typeof isEmpty;
|
|
66
|
+
isEven: (value: any) => boolean;
|
|
67
|
+
isFalsy: (value: unknown) => boolean;
|
|
68
|
+
isNaN: typeof isNaN;
|
|
69
|
+
isNegative: typeof isNegative;
|
|
70
|
+
isNotArray: (value: unknown) => boolean;
|
|
71
|
+
isNotBetween: (value: string | number, min: string | number, max: string | number) => boolean;
|
|
72
|
+
isNotBlank: (value: unknown) => boolean;
|
|
73
|
+
isNotBoolean: (value: unknown) => boolean;
|
|
74
|
+
isNotEmpty: (value: unknown) => boolean;
|
|
75
|
+
isNotNaN: (value: unknown) => boolean;
|
|
76
|
+
isNotNull: (value: unknown) => boolean;
|
|
77
|
+
isNotNullish: (value: any) => boolean;
|
|
78
|
+
isNotNumber: (value: unknown) => boolean;
|
|
79
|
+
isNotNumeric: (value: string | number) => boolean;
|
|
80
|
+
isNotString: (v: unknown) => boolean;
|
|
81
|
+
isNotUndefined: (value?: unknown) => boolean;
|
|
82
|
+
isNull: typeof isNull;
|
|
83
|
+
isNullish: typeof isNullish;
|
|
84
|
+
isNumber: typeof isNumber;
|
|
85
|
+
isNumeric: typeof isNumeric;
|
|
86
|
+
isOdd: (value: any) => boolean;
|
|
87
|
+
isPositive: (value: string | number) => boolean;
|
|
88
|
+
isString: typeof isStringValue;
|
|
89
|
+
isTruthy: typeof isTruthy;
|
|
90
|
+
isUndefined: typeof isUndefined;
|
|
91
|
+
lengthEquals: typeof lengthEquals;
|
|
92
|
+
lengthNotEquals: (value: string | unknown[], arg1: string | number) => boolean;
|
|
93
|
+
lessThan: typeof lessThan;
|
|
94
|
+
lessThanOrEquals: typeof lessThanOrEquals;
|
|
95
|
+
longerThan: typeof longerThan;
|
|
96
|
+
longerThanOrEquals: typeof longerThanOrEquals;
|
|
97
|
+
lt: typeof lessThan;
|
|
98
|
+
lte: typeof lessThanOrEquals;
|
|
99
|
+
matches: typeof matches;
|
|
100
|
+
notEquals: (value: unknown, arg1: unknown) => boolean;
|
|
101
|
+
notInside: (value: unknown, arg1: string | unknown[]) => boolean;
|
|
102
|
+
notMatches: (value: string, regex: string | RegExp) => boolean;
|
|
103
|
+
numberEquals: typeof numberEquals;
|
|
104
|
+
numberNotEquals: (value: string | number, eq: string | number) => boolean;
|
|
105
|
+
shorterThan: typeof shorterThan;
|
|
106
|
+
shorterThanOrEquals: typeof shorterThanOrEquals;
|
|
107
|
+
startsWith: typeof startsWith;
|
|
108
|
+
};
|
|
109
|
+
type TRules<E = Record<string, unknown>> = n4s.EnforceCustomMatchers<TRules<E> & E> & Record<string, (...args: TArgs) => TRules<E> & E> & {
|
|
110
|
+
[P in KBaseRules]: (...args: DropFirst<Parameters<TBaseRules[P]>> | TArgs) => TRules<E> & E;
|
|
111
|
+
};
|
|
112
|
+
/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-empty-interface */
|
|
113
|
+
declare global {
|
|
114
|
+
namespace n4s {
|
|
115
|
+
interface IRules<E> extends TRules<E> {
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
type TLazyRules = n4s.IRules<TLazyRuleMethods>;
|
|
120
|
+
type TLazy = TLazyRules & TLazyRuleMethods &
|
|
121
|
+
// This is a "catch all" hack to make TS happy while not
|
|
122
|
+
// losing type hints
|
|
123
|
+
Record<string, (...args: any[]) => any>;
|
|
124
|
+
interface IShapeObject extends Record<string, any>, Record<string, TLazyRuleRunners> {
|
|
125
|
+
}
|
|
126
|
+
type TLazyRuleMethods = TLazyRuleRunners & {
|
|
127
|
+
message: (message: TLazyMessage) => TLazy;
|
|
128
|
+
};
|
|
129
|
+
type TLazyRuleRunners = {
|
|
130
|
+
test: (value: unknown) => boolean;
|
|
131
|
+
run: (value: unknown) => TRuleDetailedResult;
|
|
132
|
+
};
|
|
133
|
+
type TLazyMessage = string | ((value: unknown, originalMessage?: TStringable) => string);
|
|
134
|
+
declare function isArrayOf(inputArray: any[], currentRule: TLazy): TRuleDetailedResult;
|
|
135
|
+
declare function loose(inputObject: Record<string, any>, shapeObject: IShapeObject): TRuleDetailedResult;
|
|
136
|
+
declare function optional(value: any, ruleChain: TLazy): TRuleDetailedResult;
|
|
137
|
+
declare function shape(inputObject: Record<string, any>, shapeObject: IShapeObject): TRuleDetailedResult;
|
|
138
|
+
// Help needed improving the typings of this file.
|
|
139
|
+
// Ideally, we'd be able to extend IShapeObject, but that's not possible.
|
|
140
|
+
declare function partial<T extends Record<any, any>>(shapeObject: T): T;
|
|
141
|
+
declare global {
|
|
142
|
+
namespace n4s {
|
|
143
|
+
interface EnforceCustomMatchers<R> {
|
|
144
|
+
isArrayOf: (...args: DropFirst<Parameters<typeof isArrayOf>>) => R;
|
|
145
|
+
loose: (...args: DropFirst<Parameters<typeof loose>>) => R;
|
|
146
|
+
shape: (...args: DropFirst<Parameters<typeof shape>>) => R;
|
|
147
|
+
optional: (...args: DropFirst<Parameters<typeof optional>>) => R;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export { partial };
|
package/config/rollup/enforce.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const addEsmDir = require('../../../../scripts/build/addEsmDir');
|
|
2
|
-
const genConfig = require('../../../../scripts/build/genConfig');
|
|
3
|
-
const writeMainTemplate = require('../../../../scripts/build/writeMainTemplate');
|
|
4
|
-
const { PACKAGE_N4S } = require('../../../../shared/constants');
|
|
5
|
-
const { packageDist } = require('../../../../util');
|
|
6
|
-
|
|
7
|
-
addEsmDir(packageDist(PACKAGE_N4S));
|
|
8
|
-
writeMainTemplate(packageDist(PACKAGE_N4S), PACKAGE_N4S);
|
|
9
|
-
|
|
10
|
-
export default genConfig({
|
|
11
|
-
libraryName: PACKAGE_N4S,
|
|
12
|
-
distPath: packageDist(PACKAGE_N4S),
|
|
13
|
-
});
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const genConfig = require('../../../../scripts/build/genConfig');
|
|
2
|
-
const { PACKAGE_N4S } = require('../../../../shared/constants');
|
|
3
|
-
const { packageDist } = require('../../../../util');
|
|
4
|
-
|
|
5
|
-
export default genConfig({
|
|
6
|
-
distPath: packageDist(PACKAGE_N4S),
|
|
7
|
-
input: 'extended/enforce/index.js',
|
|
8
|
-
libraryName: 'enforceExtended',
|
|
9
|
-
packageName: PACKAGE_N4S,
|
|
10
|
-
});
|
package/config/rollup/ensure.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const genConfig = require('../../../../scripts/build/genConfig');
|
|
2
|
-
const { PACKAGE_N4S } = require('../../../../shared/constants');
|
|
3
|
-
const { packageDist } = require('../../../../util');
|
|
4
|
-
|
|
5
|
-
export default genConfig({
|
|
6
|
-
distPath: packageDist(PACKAGE_N4S),
|
|
7
|
-
input: 'ensure/index.js',
|
|
8
|
-
libraryName: 'ensure',
|
|
9
|
-
packageName: PACKAGE_N4S,
|
|
10
|
-
});
|
package/docs/business_rules.md
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
# Business related rules
|
|
2
|
-
|
|
3
|
-
Along with the existing rules, you might need different business related rules, for email, phone number, credit card validations, and more.
|
|
4
|
-
|
|
5
|
-
n4s uses [validator.js](https://github.com/validatorjs/validator.js) to provide some of these validations.
|
|
6
|
-
|
|
7
|
-
## Validator.js validations currently provided:
|
|
8
|
-
|
|
9
|
-
- isAlphanumeric
|
|
10
|
-
- isCreditCard
|
|
11
|
-
- isCurrency
|
|
12
|
-
- isEmail
|
|
13
|
-
- isIP
|
|
14
|
-
- isIdentityCard
|
|
15
|
-
- isJSON
|
|
16
|
-
- isLocale
|
|
17
|
-
- isMimeType
|
|
18
|
-
- isMobilePhone
|
|
19
|
-
- isPassportNumber
|
|
20
|
-
- isPostalCode
|
|
21
|
-
- isURL
|
|
22
|
-
|
|
23
|
-
Because these rules are business specific, and can have an impact on your bundle size, they are split into a their own bundle.
|
|
24
|
-
|
|
25
|
-
```js
|
|
26
|
-
import enforce from 'n4s/enforceExtended';
|
|
27
|
-
|
|
28
|
-
enforce('example@gmail.com').isEmail();
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
```js
|
|
32
|
-
import ensure from 'n4s/ensureExtended';
|
|
33
|
-
|
|
34
|
-
ensure().isEmail().test('example@gmail.com');
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
Some of these rules also require locale arrays and objects. These are also exported by the extended entry point:
|
|
38
|
-
|
|
39
|
-
```js
|
|
40
|
-
import enforce, {
|
|
41
|
-
isAlphanumericLocales,
|
|
42
|
-
isMobilePhoneLocales,
|
|
43
|
-
isPostalCodeLocales,
|
|
44
|
-
} from 'n4s/enforceExtended';
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
```js
|
|
48
|
-
import ensure, {
|
|
49
|
-
isAlphanumericLocales,
|
|
50
|
-
isMobilePhoneLocales,
|
|
51
|
-
isPostalCodeLocales,
|
|
52
|
-
} from 'n4s/ensureExtended';
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
To read the full documentation on these rules and the options they take, please visit [validator.js](https://github.com/validatorjs/validator.js).
|
|
56
|
-
|
|
57
|
-
### validator.js license:
|
|
58
|
-
|
|
59
|
-
```
|
|
60
|
-
Copyright (c) 2018 Chris O'Hara <cohara87@gmail.com>
|
|
61
|
-
|
|
62
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
|
63
|
-
a copy of this software and associated documentation files (the
|
|
64
|
-
"Software"), to deal in the Software without restriction, including
|
|
65
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
|
66
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
|
67
|
-
permit persons to whom the Software is furnished to do so, subject to
|
|
68
|
-
the following conditions:
|
|
69
|
-
|
|
70
|
-
The above copyright notice and this permission notice shall be
|
|
71
|
-
included in all copies or substantial portions of the Software.
|
|
72
|
-
|
|
73
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
74
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
75
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
76
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
77
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
78
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
79
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
80
|
-
```
|
package/docs/custom.md
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
# Custom enforce rules
|
|
2
|
-
|
|
3
|
-
To make it easier to reuse logic across your application, sometimes you would want to encapsulate bits of logic in rules that you can use later on, for example, "what's considered a valid email".
|
|
4
|
-
|
|
5
|
-
Rules are called with the argument passed to enforce(x) followed by the arguments passed to `.yourRule(y, z)`.
|
|
6
|
-
|
|
7
|
-
```js
|
|
8
|
-
enforce.extend({
|
|
9
|
-
yourRule(x, y, z) {
|
|
10
|
-
return {
|
|
11
|
-
pass: true,
|
|
12
|
-
message: () => '',
|
|
13
|
-
};
|
|
14
|
-
},
|
|
15
|
-
});
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
```js
|
|
19
|
-
import { enforce } from 'n4s';
|
|
20
|
-
|
|
21
|
-
enforce.extend({
|
|
22
|
-
isValidEmail: value => value.indexOf('@') > -1,
|
|
23
|
-
hasKey: (value, key) => value.hasOwnProperty(key),
|
|
24
|
-
passwordsMatch: (passConfirm, options) =>
|
|
25
|
-
passConfirm === options.passConfirm && options.passIsValid,
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
enforce(user.email).isValidEmail();
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Custom rules return value
|
|
32
|
-
|
|
33
|
-
Rules can either return boolean indicating success or failure, or an object with two keys. `pass` indicates whether the validation is successful or not, and message provides a function with no arguments that return an error message in case of failure. Thus, when pass is false, message should return the error message for when enforce(x).yourRule() fails.
|
|
34
|
-
|
|
35
|
-
```js
|
|
36
|
-
enforce.extend({
|
|
37
|
-
isWithinRange(received, floor, ceiling) {
|
|
38
|
-
const pass = received >= floor && received <= ceiling;
|
|
39
|
-
if (pass) {
|
|
40
|
-
return {
|
|
41
|
-
message: () =>
|
|
42
|
-
`expected ${received} not to be within range ${floor} - ${ceiling}`,
|
|
43
|
-
pass: true,
|
|
44
|
-
};
|
|
45
|
-
} else {
|
|
46
|
-
return {
|
|
47
|
-
message: () =>
|
|
48
|
-
`expected ${received} to be within range ${floor} - ${ceiling}`,
|
|
49
|
-
pass: false,
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
});
|
|
54
|
-
```
|
package/docs/ensure.md
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# Not-throwing validations (ensure)
|
|
2
|
-
|
|
3
|
-
If you do not use a validations testing library but still want to use enforce [rules](./rules), you can use the ensure interface.
|
|
4
|
-
|
|
5
|
-
Ensure is similar to enforce, only that it returns a boolean value instead of throwing an error.
|
|
6
|
-
|
|
7
|
-
Since ensure has no way of knowing when you are done adding rules, you have to do that by putting the `.test()` function last with your value as its argument.
|
|
8
|
-
|
|
9
|
-
## Usage example
|
|
10
|
-
|
|
11
|
-
For example, if I want to test that the number 4 is both numeric, and is less than 5, I would:
|
|
12
|
-
|
|
13
|
-
```js
|
|
14
|
-
import { ensure } from 'n4s';
|
|
15
|
-
|
|
16
|
-
ensure().isNumeric().lessThan(5).test(4); // The value I am testing.
|
|
17
|
-
// `.test()` is always the last function passed
|
|
18
|
-
|
|
19
|
-
// returns true
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## Custom rules
|
|
23
|
-
|
|
24
|
-
You can [extend](./custom) ensure in the same way you extend enforce.
|
|
25
|
-
|
|
26
|
-
```js
|
|
27
|
-
import ensure from 'n4s';
|
|
28
|
-
|
|
29
|
-
ensure.extend({
|
|
30
|
-
isValidEmail: value => value.includes('@'),
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
ensure().isValidEmail().test('some invali dstring');
|
|
34
|
-
|
|
35
|
-
// returns false
|
|
36
|
-
|
|
37
|
-
ensure().isValidEmail().test('valid@email.com');
|
|
38
|
-
|
|
39
|
-
// returns true
|
|
40
|
-
```
|