Skip to content

Commit

Permalink
Merge 4c787e6 into bbdbd0b
Browse files Browse the repository at this point in the history
  • Loading branch information
depfu[bot] committed May 18, 2021
2 parents bbdbd0b + 4c787e6 commit 9ecd3e5
Show file tree
Hide file tree
Showing 17 changed files with 403 additions and 405 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
language: node_js
node_js:
- 12
- 14
- 16
script: npm run coverage
631 changes: 319 additions & 312 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -49,17 +49,17 @@
],
"devDependencies": {
"@babel/cli": "^7.13.16",
"@babel/core": "^7.14.0",
"@babel/core": "^7.14.2",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/plugin-proposal-decorators": "^7.13.15",
"@babel/plugin-proposal-object-rest-spread": "^7.13.8",
"@babel/preset-env": "^7.14.1",
"@babel/plugin-proposal-decorators": "^7.14.2",
"@babel/plugin-proposal-object-rest-spread": "^7.14.2",
"@babel/preset-env": "^7.14.2",
"@babel/preset-typescript": "^7.13.0",
"@types/chai": "^4.2.18",
"@types/chai-as-promised": "^7.1.4",
"@types/mocha": "^8.2.2",
"@typescript-eslint/eslint-plugin": "^4.22.1",
"@typescript-eslint/parser": "^4.22.1",
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
"babel-plugin-transform-typescript-metadata": "^0.3.2",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
Expand All @@ -70,7 +70,7 @@
"husky": "^6.0.0",
"mocha": "^8.4.0",
"nyc": "^15.1.0",
"prettier": "^2.2.1",
"prettier": "^2.3.0",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
},
Expand Down
2 changes: 1 addition & 1 deletion src/DateType.ts
Expand Up @@ -5,7 +5,7 @@ import FunctionType, { FunctionParameters } from './schema/FunctionType';
import { NumberValidator } from './number';

export class DateValidator<
P extends FunctionParameters = [Date]
P extends FunctionParameters = [Date],
> extends Validator<FunctionType<Date, P>> {
public toISOString(
...args: Parameters<Date['toISOString']>
Expand Down
6 changes: 3 additions & 3 deletions src/Schema.ts
Expand Up @@ -371,9 +371,9 @@ schema.either = function <A, S>(
>
>
> {
return (new Validator(
return new Validator(
eitherSchema(...(candidates as [unknown])),
).proxy() as unknown) as ValidatorProxy<
).proxy() as unknown as ValidatorProxy<
Validator<
FunctionType<
SchemaReturnType<A> | SchemaReturnType<S>,
Expand All @@ -393,7 +393,7 @@ schema.merge = function (

schema.enum = function <
E extends Enum<E>,
P extends FunctionParameters = [E[keyof E]]
P extends FunctionParameters = [E[keyof E]],
>(
value: E,
error?: ErrorLike<P>,
Expand Down
10 changes: 5 additions & 5 deletions src/Validator.ts
Expand Up @@ -10,12 +10,12 @@ import { MergeSchemaParameters } from './schema/io';

export type ValidatorProxy<
V extends { validator: FunctionType },
F extends FunctionType = V['validator']
F extends FunctionType = V['validator'],
> = Omit<V, 'validator' | 'proxy'> & { validator: F } & F;

export interface ValidatorConstructor<
V extends Validator<F>,
F extends FunctionType = V['validator']
F extends FunctionType = V['validator'],
> {
new (validator: F): V;
}
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class Validator<F extends FunctionType> {
T,
V extends Validator<
FunctionType<MaybeAsync<ReturnType<F>, T>, Parameters<F>>
>
>,
>(
fn: FunctionType<T, [ResolvedValue<ReturnType<F>>]>,
constructor: ValidatorConstructor<V> = this
Expand Down Expand Up @@ -91,7 +91,7 @@ export default class Validator<F extends FunctionType> {
}

public optional<
R extends ResolvedValue<ReturnType<F>> | undefined = undefined
R extends ResolvedValue<ReturnType<F>> | undefined = undefined,
>(
defaultValue?: R,
): ValidatorProxy<
Expand All @@ -109,7 +109,7 @@ export default class Validator<F extends FunctionType> {
}

public strictOptional<
R extends ResolvedValue<ReturnType<F>> | undefined = undefined
R extends ResolvedValue<ReturnType<F>> | undefined = undefined,
>(
defaultValue?: R,
): ValidatorProxy<
Expand Down
6 changes: 3 additions & 3 deletions src/array.ts
Expand Up @@ -12,7 +12,7 @@ import { isPromiseLike, ResolvedValue } from './schema/utils';

export class ArrayValidator<
R extends unknown[] | PromiseLike<unknown[]> = unknown[],
P extends FunctionParameters = [R]
P extends FunctionParameters = [R],
> extends Validator<FunctionType<R, P>> {
public of<S>(
schema: S,
Expand All @@ -22,7 +22,7 @@ export class ArrayValidator<
> {
const validator = compiler(schema, { error });

return (this.transform((arr: ResolvedValue<R>):
return this.transform((arr: ResolvedValue<R>):
| PromiseLike<SchemaResolveType<S>[]>
| SchemaResolveType<S>[] => {
let isAsync;
Expand All @@ -42,7 +42,7 @@ export class ArrayValidator<
}

return Promise.all(items);
}) as unknown) as ValidatorProxy<
}) as unknown as ValidatorProxy<
ArrayValidator<SchemaReturnType<S, SchemaResolveType<S>[]>, P>
>;
}
Expand Down
2 changes: 1 addition & 1 deletion src/boolean.ts
Expand Up @@ -3,7 +3,7 @@ import FunctionType, { FunctionParameters } from './schema/FunctionType';
import { type } from './schema/validations';

export class BooleanValidator<
P extends FunctionParameters = [boolean]
P extends FunctionParameters = [boolean],
> extends Validator<FunctionType<boolean, P>> {}

const boolean = new BooleanValidator(type('boolean')).proxy();
Expand Down
2 changes: 1 addition & 1 deletion src/number.ts
Expand Up @@ -5,7 +5,7 @@ import FunctionType, { FunctionParameters } from './schema/FunctionType';
import { type } from './schema/validations';

export class NumberValidator<
P extends FunctionParameters = [number]
P extends FunctionParameters = [number],
> extends Validator<FunctionType<number, P>> {
public float(error?: ErrorLike<[number]>): ValidatorProxy<this> {
return this.test(
Expand Down
2 changes: 1 addition & 1 deletion src/object.ts
Expand Up @@ -4,7 +4,7 @@ import { type } from './schema/validations';

export class ObjectValidator<
// eslint-disable-next-line @typescript-eslint/ban-types
P extends FunctionParameters = [object]
P extends FunctionParameters = [object],
// eslint-disable-next-line @typescript-eslint/ban-types
> extends Validator<FunctionType<object, P>> {}

Expand Down
15 changes: 6 additions & 9 deletions src/schema/compiler.test.ts
Expand Up @@ -41,7 +41,7 @@ describe('schema', () => {
typeCheck<typeof ret, number>('ok');
assert.equal(ret, 3);

assert.equal(validator((true as unknown) as number), 2);
assert.equal(validator(true as unknown as number), 2);
});

it('optional validator', () => {
Expand Down Expand Up @@ -147,10 +147,10 @@ describe('schema', () => {
});
typeCheck<
typeof validator,
(x: {
foo?: any;
bar: { hello?: any; world?: unknown };
}) => { foo: string; bar: { hello: string; world: boolean } }
(x: { foo?: any; bar: { hello?: any; world?: unknown } }) => {
foo: string;
bar: { hello: string; world: boolean };
}
>('ok');

assert.deepEqual(validator({ foo: 'foo', bar: { hello: 'hi' } }), {
Expand Down Expand Up @@ -220,10 +220,7 @@ describe('schema', () => {
});
typeCheck<
typeof validator,
(x: {
foo?: any;
bar: { hello: number; world?: any };
}) => PromiseLike<{
(x: { foo?: any; bar: { hello: number; world?: any } }) => PromiseLike<{
foo: string;
bar: { hello: number; world: number };
}>
Expand Down
68 changes: 33 additions & 35 deletions src/schema/compiler.ts
Expand Up @@ -32,7 +32,7 @@ export default function compiler<S>(
const { error, basePath = [], strict } = opts || {};

if (typeof schema === 'function') {
return (schema as unknown) as SchemaValidatorFunction<S>;
return schema as unknown as SchemaValidatorFunction<S>;
}

if (typeof schema !== 'object' || schema === null) {
Expand All @@ -56,7 +56,7 @@ export default function compiler<S>(
...args: SchemaParameters<S>
): // eslint-disable-next-line @typescript-eslint/ban-types
[object, Record<string, unknown>] => {
return [validator(...args), ([] as unknown) as Record<string, unknown>];
return [validator(...args), [] as unknown as Record<string, unknown>];
};
} else {
const validator = type('object', error);
Expand All @@ -71,42 +71,40 @@ export default function compiler<S>(

const keys = Object.keys(schema);

const tasks = keys.map(
(key): SchemaKeyTask => {
const path = [...basePath, key];
const validator = compiler<unknown>(schema[key as keyof S], {
basePath: path,
strict,
});
const tasks = keys.map((key): SchemaKeyTask => {
const path = [...basePath, key];
const validator = compiler<unknown>(schema[key as keyof S], {
basePath: path,
strict,
});

return (res, errors, obj): void | PromiseLike<void> => {
try {
const value: unknown = validator(
(obj as { [key: string]: unknown })[key],
);
return (res, errors, obj): void | PromiseLike<void> => {
try {
const value: unknown = validator(
(obj as { [key: string]: unknown })[key],
);

if (!isPromiseLike(value)) {
res[key] = value;
return;
}

return value.then(
(value) => {
res[key] = value;
},
(error) => {
errors.push({ error, path });
},
);
} catch (error) {
errors.push({
error,
path,
});
if (!isPromiseLike(value)) {
res[key] = value;
return;
}
};
},
);

return value.then(
(value) => {
res[key] = value;
},
(error) => {
errors.push({ error, path });
},
);
} catch (error) {
errors.push({
error,
path,
});
}
};
});

if (strict) {
const keysSet = new Set(keys);
Expand Down
14 changes: 6 additions & 8 deletions src/schema/io.ts
Expand Up @@ -66,14 +66,12 @@ type IsSchemaAsync<S> = S extends FunctionType
: { [K in keyof S]: IsSchemaAsync<S[K]> }[keyof S]
: IsAsync<S>;

export type SchemaReturnType<
S,
R = SchemaResolveType<S>
> = unknown extends IsSchemaAsync<S>
? PromiseLike<R> | R
: true extends IsSchemaAsync<S>
? PromiseLike<R>
: R;
export type SchemaReturnType<S, R = SchemaResolveType<S>> =
unknown extends IsSchemaAsync<S>
? PromiseLike<R> | R
: true extends IsSchemaAsync<S>
? PromiseLike<R>
: R;

export type SchemaValidatorFunction<S> = FunctionType<
SchemaReturnType<S>,
Expand Down
18 changes: 8 additions & 10 deletions src/schema/logic.test.ts
Expand Up @@ -14,7 +14,7 @@ use(chaiAsPromised);
describe('schema/logic', () => {
describe('either', () => {
it('no candidates', () => {
assert.throw(() => either(...(([] as unknown) as [unknown])), RangeError);
assert.throw(() => either(...([] as unknown as [unknown])), RangeError);
});

it('sync candidate', () => {
Expand All @@ -32,15 +32,13 @@ describe('schema/logic', () => {
});

it('async candidate', async () => {
const validator = either(
async (x: number): Promise<string> => {
if (x <= 0) {
throw new RangeError(`Negative input`);
}
const validator = either(async (x: number): Promise<string> => {
if (x <= 0) {
throw new RangeError(`Negative input`);
}

return String(x);
},
);
return String(x);
});

typeCheck<typeof validator, (x: number) => PromiseLike<string>>('ok');

Expand Down Expand Up @@ -173,7 +171,7 @@ describe('schema/logic', () => {

describe('merge', () => {
it('no items', () => {
assert.throw(() => merge(...(([] as unknown) as [unknown])), RangeError);
assert.throw(() => merge(...([] as unknown as [unknown])), RangeError);
});

it('one item', () => {
Expand Down
10 changes: 5 additions & 5 deletions src/schema/validations.ts
Expand Up @@ -10,7 +10,7 @@ import {

export function type<
T extends keyof Typeof,
P extends FunctionParameters = [Typeof[T]]
P extends FunctionParameters = [Typeof[T]],
>(type: T, error?: ErrorLike<P>): FunctionType<Typeof[T], P> {
return (...args: P): Typeof[T] => {
if (typeof args[0] !== type || args[0] === null) {
Expand Down Expand Up @@ -92,9 +92,9 @@ export function error<R, P extends FunctionParameters>(
return res;
}

return (res.then(null, (): never => {
return res.then(null, (): never => {
throw toError(err, ...args);
}) as unknown) as R;
}) as unknown as R;
} catch (e) {
throw toError(err, ...args);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ export function regexp<P extends FunctionParameters = [string]>(

export function array<
R extends Array<unknown>,
P extends FunctionParameters = [R]
P extends FunctionParameters = [R],
>(length: number | null = null, error?: ErrorLike<P>): FunctionType<R, P> {
const isArray = (...args: P): R => {
if (!Array.isArray(args[0])) {
Expand Down Expand Up @@ -153,7 +153,7 @@ export function array<

export function enumValue<
E extends Enum<E>,
P extends FunctionParameters = [E[keyof E]]
P extends FunctionParameters = [E[keyof E]],
>(value: E, error?: ErrorLike<P>): FunctionType<E[keyof E], P> {
const values = new Set<E[keyof E]>(
Object.keys(value)
Expand Down

0 comments on commit 9ecd3e5

Please sign in to comment.