Skip to content

Commit

Permalink
types: Update schema validation to use unknown.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Sep 14, 2021
1 parent a2a1e4f commit 72269c4
Show file tree
Hide file tree
Showing 19 changed files with 12 additions and 59 deletions.
8 changes: 4 additions & 4 deletions optimal/src/createSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
function validate<T>(
state: SchemaState<T>,
validators: Criteria<T>[],
initialValue: T | null | undefined,
initialValue: unknown,
path: string = '',
{
collectErrors = true,
Expand All @@ -30,7 +30,7 @@ function validate<T>(
): T | null {
const { defaultValue, metadata } = state;

let value: T | null | undefined = initialValue;
let value: unknown = initialValue;

// Handle undefined
if (value === undefined) {
Expand Down Expand Up @@ -67,7 +67,7 @@ function validate<T>(

tryAndCollect(
() => {
const result = test.validate(value!, path, {
const result = test.validate(value as T, path, {
collectErrors,
currentObject,
rootObject,
Expand All @@ -86,7 +86,7 @@ function validate<T>(
throw optimalError;
}

return value!;
return value as T;
}

export function createSchema<S extends AnySchema, T = InferSchemaType<S>>(
Expand Down
4 changes: 2 additions & 2 deletions optimal/src/schemas/date.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { createSchema } from '../createSchema';
import { commonCriteria, dateCriteria } from '../criteria';
import { createDate, invalid, isValidDate } from '../helpers';
import { CommonCriterias, DateCriterias, DefaultValue, MaybeDate, Schema } from '../types';
import { CommonCriterias, DateCriterias, DefaultValue, Schema } from '../types';

export interface DateSchema<T = Date>
extends Schema<T, MaybeDate>,
extends Schema<T>,
DateCriterias<DateSchema<T>>,
CommonCriterias<DateSchema<T>> {
never: () => DateSchema<never>;
Expand Down
2 changes: 1 addition & 1 deletion optimal/src/schemas/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createObject, invalid, isObject } from '../helpers';
import { Blueprint, CommonCriterias, InferNullable, Schema, ShapeCriterias } from '../types';

export interface ShapeSchema<T>
extends Schema<T, Partial<T>>,
extends Schema<T>,
ShapeCriterias<ShapeSchema<T>>,
CommonCriterias<ShapeSchema<T>> {
never: () => ShapeSchema<never>;
Expand Down
10 changes: 3 additions & 7 deletions optimal/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,10 @@ export interface SchemaValidateOptions {
rootObject?: UnknownObject;
}

export interface Schema<Output, Input = Output> {
export interface Schema<Output> {
schema: () => string;
type: () => string;
validate: (
value: Input | null | undefined,
path?: string,
options?: SchemaValidateOptions,
) => Output;
validate: (value: unknown, path?: string, options?: SchemaValidateOptions) => Output;
}

export interface SchemaState<T> {
Expand Down Expand Up @@ -180,6 +176,6 @@ export type DeepPartial<T> = T extends Function

// Any is required for generics to be typed correctly for consumers
/* eslint-disable @typescript-eslint/no-explicit-any */
export type AnySchema = Schema<any, any>;
export type AnySchema = Schema<any>;

export type AnyFunction = (...args: any[]) => any;
2 changes: 0 additions & 2 deletions optimal/tests/schemas/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,12 @@ describe('array()', () => {

it('errors if a non-array is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be an array.');
});

it('errors if array value type is invalid', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate([123]);
}).toThrow('Must be a string.');
});
Expand Down
2 changes: 0 additions & 2 deletions optimal/tests/schemas/blueprint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ describe('blueprint()', () => {
describe('validateType()', () => {
it('errors if a non-object is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a plain object.');
});

it('errors if a value is not a schema', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate({ foo: 123 });
}).toThrow('Must be a schema.');
});
Expand Down
3 changes: 0 additions & 3 deletions optimal/tests/schemas/bool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe('bool()', () => {

it('errors if value is `true`', () => {
expect(() => {
// @ts-expect-error Invalid type
falseSchema.validate(true);
}).toThrow('May only be `false`.');
});
Expand Down Expand Up @@ -62,7 +61,6 @@ describe('bool()', () => {

it('errors if value is `false`', () => {
expect(() => {
// @ts-expect-error Invalid type
trueSchema.validate(false);
}).toThrow('May only be `true`.');
});
Expand Down Expand Up @@ -95,7 +93,6 @@ describe('bool()', () => {
describe('validateType()', () => {
it('errors if a non-boolean is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a boolean.');
});
Expand Down
1 change: 0 additions & 1 deletion optimal/tests/schemas/custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe('custom()', () => {

it('errors if a non-string is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a string!!!');
});
Expand Down
1 change: 0 additions & 1 deletion optimal/tests/schemas/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ describe('date()', () => {
describe('validateType()', () => {
it('errors if a non-date value is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate([]);
}).toThrow('Must be a string, number, or `Date` that resolves to a valid date.');
});
Expand Down
3 changes: 1 addition & 2 deletions optimal/tests/schemas/func.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnyFunction,func, FunctionSchema, Infer } from '../../src';
import { AnyFunction, func, FunctionSchema, Infer } from '../../src';
import { runCommonTests } from './runCommonTests';

describe('func()', () => {
Expand Down Expand Up @@ -27,7 +27,6 @@ describe('func()', () => {
describe('validateType()', () => {
it('errors if a non-function is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a function.');
});
Expand Down
1 change: 0 additions & 1 deletion optimal/tests/schemas/lazy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ describe('lazy()', () => {

it('errors if valid values the lazy schema', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a string.');

Expand Down
2 changes: 0 additions & 2 deletions optimal/tests/schemas/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('number()', () => {

it('errors if number is not in list', () => {
expect(() => {
// @ts-expect-error Invalid type
oneOfSchema.validate(5);
}).toThrow('Number must be one of: 1, 2, 3');
});
Expand Down Expand Up @@ -321,7 +320,6 @@ describe('number()', () => {
describe('validateType()', () => {
it('errors if a non-number is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate('abc');
}).toThrow('Must be a number.');
});
Expand Down
2 changes: 0 additions & 2 deletions optimal/tests/schemas/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,12 @@ describe('object()', () => {

it('errors if a non-object is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a plain object.');
});

it('errors if object value type is invalid', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate({ a: 123 });
}).toThrow('Must be a string.');
});
Expand Down
2 changes: 0 additions & 2 deletions optimal/tests/schemas/regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ describe('regex()', () => {
describe('validateType()', () => {
it('errors if a non-object is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be an instance of RegExp.');
});

it('errors if a plain object is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate({});
}).toThrow('Must be an instance of RegExp.');
});
Expand Down
4 changes: 0 additions & 4 deletions optimal/tests/schemas/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe('schema()', () => {
describe('validateType()', () => {
it('errors if a non-object is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a schema.');
});
Expand All @@ -36,21 +35,18 @@ describe('schema()', () => {

it('errors if a schema is not a function', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate({ schema: 123 });
}).toThrow('Must be a function.');
});

it('errors if a type is not a function', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate({ schema() {}, type: 123 });
}).toThrow('Must be a function.');
});

it('errors if a validate is not a function', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate({ schema() {}, type() {}, validate: 123 });
}).toThrow('Must be a function.');
});
Expand Down
2 changes: 0 additions & 2 deletions optimal/tests/schemas/shape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ describe('shape()', () => {

it('errors if unknown properties passed', () => {
expect(() => {
// @ts-expect-error Invalid type
exactSchema.validate({ foo: '', bar: 0, baz: true, qux: 'unknown' });
}).toThrow('Unknown fields: qux.');
});
Expand Down Expand Up @@ -107,7 +106,6 @@ describe('shape()', () => {

it('errors if a non-shape is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a shaped object.');
});
Expand Down
2 changes: 0 additions & 2 deletions optimal/tests/schemas/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ describe('string()', () => {

it('errors if value is not in the list', () => {
expect(() => {
// @ts-expect-error Invalid type
oneOfSchema.validate('qux');
}).toThrowErrorMatchingInlineSnapshot(`"String must be one of: foo, bar, baz"`);
});
Expand Down Expand Up @@ -571,7 +570,6 @@ describe('string()', () => {
describe('validateType()', () => {
it('errors if a non-string is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a string.');
});
Expand Down
4 changes: 0 additions & 4 deletions optimal/tests/schemas/tuple.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ describe('tuple()', () => {
describe('validateType()', () => {
it('errors if a non-tuple is passed', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate(123);
}).toThrow('Must be a tuple.');
});

it('errors if the first tuple item is invalid', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate([null, false, 4, {}, 'foo']);
}).toThrow('Null is not allowed.');
});
Expand All @@ -78,7 +76,6 @@ describe('tuple()', () => {

it('errors if the last tuple item is invalid', () => {
expect(() => {
// @ts-expect-error Invalid type
schema.validate([[], false, 4, {}, 'qux']);
}).toThrow('String must be one of: foo, bar, baz');
});
Expand Down Expand Up @@ -110,7 +107,6 @@ describe('tuple()', () => {
});

it('returns the default value from its items when an empty array is passed', () => {
// @ts-expect-error Invalid type
expect(schema.validate([])).toEqual([[], true, 1, {}, 'foo']);
});
});
Expand Down
Loading

0 comments on commit 72269c4

Please sign in to comment.