Skip to content

Commit

Permalink
breaking(vest, enforce): prepare next major
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Nov 10, 2021
1 parent 479a29d commit dab8e00
Show file tree
Hide file tree
Showing 16 changed files with 743 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/anyone/all/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"main": "../dist/cjs/all.js",
"module": "../dist/es/all.production.js",
"name": "all",
"types": "../types/all.d.ts",
"private": true
}
7 changes: 7 additions & 0 deletions packages/anyone/any/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"main": "../dist/cjs/any.js",
"module": "../dist/es/any.production.js",
"name": "any",
"types": "../types/any.d.ts",
"private": true
}
7 changes: 7 additions & 0 deletions packages/anyone/none/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"main": "../dist/cjs/none.js",
"module": "../dist/es/none.production.js",
"name": "none",
"types": "../types/none.d.ts",
"private": true
}
7 changes: 7 additions & 0 deletions packages/anyone/one/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"main": "../dist/cjs/one.js",
"module": "../dist/es/one.production.js",
"name": "one",
"types": "../types/one.d.ts",
"private": true
}
17 changes: 17 additions & 0 deletions packages/anyone/types/anyone.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Checks that at all passed arguments evaluate to a truthy value.
*/
declare function all(...args: unknown[]): boolean;
/**
* Checks that at least one passed argument evaluates to a truthy value.
*/
declare function any(...args: unknown[]): boolean;
/**
* Checks that at none of the passed arguments evaluate to a truthy value.
*/
declare function none(...args: unknown[]): boolean;
/**
* Checks that at only one passed argument evaluates to a truthy value.
*/
declare function one(...args: unknown[]): boolean;
export { all, any, none, one };
7 changes: 7 additions & 0 deletions packages/context/types/context.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare function createContext<T extends Record<string, unknown>>(init?: (ctxRef: Partial<T>, parentContext: T | void) => T | null): {
run: <R>(ctxRef: Partial<T>, fn: (context: T) => R) => R;
bind: <Fn extends (...args: any[]) => any>(ctxRef: Partial<T>, fn: Fn) => Fn;
use: () => T | undefined;
useX: (errorMessage?: string) => T;
};
export { createContext };
7 changes: 7 additions & 0 deletions packages/n4s/compose/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"main": "../dist/cjs/compose.js",
"module": "../dist/es/compose.production.js",
"name": "compose",
"types": "../types/compose.d.ts",
"private": true
}
9 changes: 9 additions & 0 deletions packages/n4s/types/compose.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type TRuleDetailedResult = {
pass: boolean;
message?: string;
};
declare function compose(...composites: any[]): ((value: any) => void) & {
run: (value: any) => TRuleDetailedResult;
test: (value: any) => boolean;
};
export { compose as default };
179 changes: 179 additions & 0 deletions packages/n4s/types/n4s.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
type TEnforceContext = null | {
meta: Record<string, any>;
value: any;
parent: () => TEnforceContext;
};
type DropFirst<T extends unknown[]> = T extends [
unknown,
...infer U
] ? U : never;
type TStringable = string | ((...args: any[]) => string);
type TRuleReturn = boolean | {
pass: boolean;
message?: TStringable;
};
type TRuleDetailedResult = {
pass: boolean;
message?: string;
};
type TLazyRules = TRules<TLazyRuleMethods>;
type TLazy = TLazyRules & TLazyRuleMethods;
type TShapeObject = Record<any, TLazy>;
type TLazyRuleMethods = TLazyRuleRunners & {
message: (message: TLazyMessage) => TLazy;
};
type TLazyRuleRunners = {
test: (value: unknown) => boolean;
run: (value: unknown) => TRuleDetailedResult;
};
type TLazyMessage = string | ((value: unknown, originalMessage?: TStringable) => string);
declare function allOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult;
declare function anyOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult;
declare function noneOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult;
declare function oneOf(value: unknown, ...rules: TLazy[]): TRuleDetailedResult;
declare function optional(value: any, ruleChain: TLazy): TRuleDetailedResult;
declare function compounds(): {
allOf: typeof allOf;
anyOf: typeof anyOf;
noneOf: typeof noneOf;
oneOf: typeof oneOf;
optional: typeof optional;
};
type TCompounds = ReturnType<typeof compounds>;
type KCompounds = keyof TCompounds;
type TArgs = any[];
type TRuleValue = any;
type TRuleBase = (value: TRuleValue, ...args: TArgs) => TRuleReturn;
type TRule = Record<string, TRuleBase>;
type TBaseRules = typeof baseRules;
type KBaseRules = keyof TBaseRules;
declare function condition(value: any, callback: (value: any) => TRuleReturn): TRuleReturn;
declare function endsWith(value: string, arg1: string): boolean;
declare function equals(value: unknown, arg1: unknown): boolean;
declare function greaterThan(value: number | string, gt: number | string): boolean;
declare function greaterThanOrEquals(value: string | number, gte: string | number): boolean;
declare function inside(value: unknown, arg1: string | unknown[]): boolean;
// The module is named "isArrayValue" since it
// is conflicting with a nested npm dependency.
// We may need to revisit this in the future.
declare function isArray(value: unknown): value is Array<unknown>;
declare function isBetween(value: number | string, min: number | string, max: number | string): boolean;
declare function isBlank(value: unknown): boolean;
declare function isBoolean(value: unknown): value is boolean;
declare function isEmpty(value: unknown): boolean;
declare function isNaN(value: unknown): boolean;
declare function isNegative(value: number | string): boolean;
declare function isNull(value: unknown): value is null;
declare function isNumber(value: unknown): value is number;
declare function isNumeric(value: string | number): boolean;
declare function isStringValue(v: unknown): v is string;
declare function isTruthy(value: unknown): boolean;
declare function isUndefined(value?: unknown): boolean;
declare function lengthEquals(value: string | unknown[], arg1: string | number): boolean;
declare function lessThan(value: string | number, lt: string | number): boolean;
declare function lessThanOrEquals(value: string | number, lte: string | number): boolean;
declare function longerThan(value: string | unknown[], arg1: string | number): boolean;
declare function longerThanOrEquals(value: string | unknown[], arg1: string | number): boolean;
declare function matches(value: string, regex: RegExp | string): boolean;
declare function numberEquals(value: string | number, eq: string | number): boolean;
declare function shorterThan(value: string | unknown[], arg1: string | number): boolean;
declare function shorterThanOrEquals(value: string | unknown[], arg1: string | number): boolean;
declare function startsWith(value: string, arg1: string): boolean;
declare function shape(inputObject: Record<string, any>, shapeObject: TShapeObject): TRuleDetailedResult;
declare function loose(inputObject: Record<string, any>, shapeObject: TShapeObject): TRuleDetailedResult;
declare function isArrayOf(inputArray: any[], currentRule: TLazy): TRuleDetailedResult;
declare const baseRules: {
condition: typeof condition;
doesNotEndWith: (value: string, arg1: string) => boolean;
doesNotStartWith: (value: string, arg1: string) => boolean;
endsWith: typeof endsWith;
equals: typeof equals;
greaterThan: typeof greaterThan;
greaterThanOrEquals: typeof greaterThanOrEquals;
gt: typeof greaterThan;
gte: typeof greaterThanOrEquals;
inside: typeof inside;
isArray: typeof isArray;
isBetween: typeof isBetween;
isBlank: typeof isBlank;
isBoolean: typeof isBoolean;
isEmpty: typeof isEmpty;
isEven: (value: any) => boolean;
isFalsy: (value: unknown) => boolean;
isNaN: typeof isNaN;
isNegative: typeof isNegative;
isNotArray: (value: unknown) => boolean;
isNotBetween: (value: string | number, min: string | number, max: string | number) => boolean;
isNotBlank: (value: unknown) => boolean;
isNotBoolean: (value: unknown) => boolean;
isNotEmpty: (value: unknown) => boolean;
isNotNaN: (value: unknown) => boolean;
isNotNull: (value: unknown) => boolean;
isNotNumber: (value: unknown) => boolean;
isNotNumeric: (value: string | number) => boolean;
isNotString: (v: unknown) => boolean;
isNotUndefined: (value?: unknown) => boolean;
isNull: typeof isNull;
isNumber: typeof isNumber;
isNumeric: typeof isNumeric;
isOdd: (value: any) => boolean;
isPositive: (value: string | number) => boolean;
isString: typeof isStringValue;
isTruthy: typeof isTruthy;
isUndefined: typeof isUndefined;
lengthEquals: typeof lengthEquals;
lengthNotEquals: (value: string | unknown[], arg1: string | number) => boolean;
lessThan: typeof lessThan;
lessThanOrEquals: typeof lessThanOrEquals;
longerThan: typeof longerThan;
longerThanOrEquals: typeof longerThanOrEquals;
lt: typeof lessThan;
lte: typeof lessThanOrEquals;
matches: typeof matches;
notEquals: (value: unknown, arg1: unknown) => boolean;
notInside: (value: unknown, arg1: string | unknown[]) => boolean;
notMatches: (value: string, regex: string | RegExp) => boolean;
numberEquals: typeof numberEquals;
numberNotEquals: (value: string | number, eq: string | number) => boolean;
shorterThan: typeof shorterThan;
shorterThanOrEquals: typeof shorterThanOrEquals;
startsWith: typeof startsWith;
} & {
allOf: typeof allOf;
anyOf: typeof anyOf;
noneOf: typeof noneOf;
oneOf: typeof oneOf;
optional: typeof optional;
} & {
shape: typeof shape;
loose: typeof loose;
isArrayOf: typeof isArrayOf;
};
/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/no-empty-interface */
declare global {
namespace n4s {
interface EnforceCustomMatchers<R> {
}
}
}
type TRules<E = Record<string, unknown>> = n4s.EnforceCustomMatchers<TRules & E> & Record<string, (...args: TArgs) => TRules & E> & {
[P in KCompounds]: (...args: DropFirst<Parameters<TCompounds[P]>> | TArgs) => TRules & E;
} & {
[P in KBaseRules]: (...args: DropFirst<Parameters<TBaseRules[P]>> | TArgs) => TRules & E;
};
declare function enforceEager(value: TRuleValue): TRules;
type TEnforceEager = typeof enforceEager;
// Help needed improving the typings of this file.
// Ideally, we'd be able to extend TShapeObject, but that's not possible.
declare function partial<T extends Record<any, any>>(shapeObject: T): T;
declare function modifiers(): {
partial: typeof partial;
};
type TModifiers = ReturnType<typeof modifiers>;
declare const enforce: TEnforce;
type TEnforce = TEnforceEager & TLazyRules & TEnforceMethods;
type TEnforceMethods = TModifiers & {
context: () => TEnforceContext;
extend: (customRules: TRule) => void;
};
export { enforce as default };
7 changes: 7 additions & 0 deletions packages/vest/classnames/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"main": "../dist/cjs/classnames.js",
"module": "../dist/es/classnames.production.js",
"name": "classnames",
"types": "../types/classnames.d.ts",
"private": true
}
7 changes: 7 additions & 0 deletions packages/vest/parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"main": "../dist/cjs/parser.js",
"module": "../dist/es/parser.production.js",
"name": "parser",
"types": "../types/parser.d.ts",
"private": true
}
7 changes: 7 additions & 0 deletions packages/vest/promisify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"main": "../dist/cjs/promisify.js",
"module": "../dist/es/promisify.production.js",
"name": "promisify",
"types": "../types/promisify.d.ts",
"private": true
}
70 changes: 70 additions & 0 deletions packages/vest/types/classnames.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Reads the testObjects list and gets full validation result from it.
*/
declare function genTestsSummary(): TTestSummary;
type TTestSummary = {
groups: Record<string, TTestGroup>;
tests: TTestGroup;
} & TTestSummaryBase;
type TTestGroup = Record<string, TSingleTestSummary>;
type TSingleTestSummary = {
errors: string[];
warnings: string[];
} & TTestSummaryBase;
type TTestSummaryBase = {
errorCount: number;
warnCount: number;
testCount: number;
};
declare function getErrors(): Record<string, string[]>;
declare function getErrors(fieldName?: string): string[];
declare function getWarnings(): Record<string, string[]>;
declare function getWarnings(fieldName?: string): string[];
declare function getErrorsByGroup(groupName: string): Record<string, string[]>;
declare function getErrorsByGroup(groupName: string, fieldName: string): string[];
declare function getWarningsByGroup(groupName: string): Record<string, string[]>;
declare function getWarningsByGroup(groupName: string, fieldName: string): string[];
declare function hasErrors(fieldName?: string): boolean;
declare function hasWarnings(fieldName?: string): boolean;
declare function hasErrorsByGroup(groupName: string, fieldName?: string): boolean;
declare function hasWarningsByGroup(groupName: string, fieldName?: string): boolean;
type TDraftResult = ReturnType<typeof genTestsSummary> & {
/**
* Returns whether the suite as a whole is valid.
* Determined if there are no errors, and if no
* required fields are skipped.
*/
isValid: (fieldName?: string) => boolean;
hasErrors: typeof hasErrors;
hasWarnings: typeof hasWarnings;
getErrors: typeof getErrors;
getWarnings: typeof getWarnings;
hasErrorsByGroup: typeof hasErrorsByGroup;
hasWarningsByGroup: typeof hasWarningsByGroup;
getErrorsByGroup: typeof getErrorsByGroup;
getWarningsByGroup: typeof getWarningsByGroup;
};
type IVestResult = TDraftResult & {
done: IDone;
};
interface IDone {
(...args: [
cb: (res: TDraftResult) => void
]): IVestResult;
(...args: [
fieldName: string,
cb: (res: TDraftResult) => void
]): IVestResult;
}
/**
* Creates a function that returns class names that match the validation result
*/
declare function classnames(res: IVestResult | TDraftResult, classes?: TSupportedClasses): (fieldName: string) => string;
type TSupportedClasses = {
valid?: string;
tested?: string;
invalid?: string;
warning?: string;
untested?: string;
};
export { classnames as default };
12 changes: 12 additions & 0 deletions packages/vest/types/enforce-compose.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type TRuleDetailedResult = {
pass: boolean;
message?: string;
};
/* eslint-disable max-lines-per-function */
// TODO: This gives me a headache. Instead of `any` we should use `TLazy`
// but it fails when using compose. The type is very complex.
declare function compose(...composites: any[]): ((value: any) => void) & {
run: (value: any) => TRuleDetailedResult;
test: (value: any) => boolean;
};
export { compose as default };
Loading

0 comments on commit dab8e00

Please sign in to comment.