Skip to content

Commit

Permalink
feat: remove unneeded Out type from schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Dec 17, 2020
1 parent 495ae84 commit 0bf9732
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 72 deletions.
11 changes: 1 addition & 10 deletions .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,5 @@ module.exports = (api) => ({
],
'@babel/preset-typescript',
],
plugins: [
'@babel/plugin-proposal-logical-assignment-operators',
api.env() === 'modules' && [
'transform-rename-import',
{
original: 'lodash',
replacement: 'lodash-es',
},
],
].filter(Boolean),
plugins: ['@babel/plugin-proposal-logical-assignment-operators'],
});
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@babel/core": "7.12.10",
"@babel/plugin-proposal-logical-assignment-operators": "^7.12.1",
"@babel/preset-typescript": "^7.12.7",
"@typescript-eslint/eslint-plugin": "^4.9.1",
"@typescript-eslint/parser": "^4.9.1",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
Expand Down Expand Up @@ -108,12 +109,9 @@
},
"dependencies": {
"@babel/runtime": "^7.12.5",
"@types/lodash": "^4.14.165",
"@typescript-eslint/eslint-plugin": "^4.9.1",
"lodash": "^4.17.20",
"lodash-es": "^4.17.11",
"nanoclone": "^0.2.1",
"property-expr": "^2.0.4",
"tiny-case": "^1.0.1",
"toposort": "^2.0.2"
},
"engines": {
Expand Down
8 changes: 1 addition & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ const base = {
presets: [['jason', { modules: false, runtime: false }]],
}),
],
external: [
'lodash/snakeCase',
'lodash/camelCase',
'toposort',
'fn-name',
'property-expr',
],
external: ['toposort', 'fn-name', 'property-expr'],
};

module.exports = [
Expand Down
10 changes: 4 additions & 6 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
Callback,
Message,
Maybe,
Optionals,
} from './types';
import ValidationError from './ValidationError';
import type Reference from './Reference';
Expand All @@ -21,7 +20,6 @@ import {
SetFlag,
Thunk,
ToggleDefault,
TypeOf,
} from './util/types';
import BaseSchema, {
AnySchema,
Expand All @@ -43,8 +41,8 @@ export function create<
export default class ArraySchema<
T extends AnySchema | Lazy<any, any>,
C extends Config<any, any> = Config,
TIn extends Maybe<TypeOf<T>[]> = TypeOf<T>[] | undefined
> extends BaseSchema<TIn, Asserts<T>[] | Optionals<TIn>, C> {
TIn extends Maybe<Asserts<T>[]> = Asserts<T>[] | undefined
> extends BaseSchema<TIn, C> {
innerType?: T;

constructor(type?: T) {
Expand Down Expand Up @@ -288,8 +286,8 @@ create.prototype = ArraySchema.prototype;
export default interface ArraySchema<
T extends AnySchema | Lazy<any, any>,
C extends Config<any, any> = Config,
TIn extends Maybe<TypeOf<T>[]> = TypeOf<T>[] | undefined
> extends BaseSchema<TIn, Asserts<T>[] | Optionals<TIn>, C> {
TIn extends Maybe<Asserts<T>[]> = Asserts<T>[] | undefined
> extends BaseSchema<TIn, C> {
default<D extends Maybe<TIn>>(
def: Thunk<D>,
): ArraySchema<T, ToggleDefault<C, D>, TIn>;
Expand Down
4 changes: 2 additions & 2 deletions src/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function create() {
export default class BooleanSchema<
TType extends Maybe<boolean> = boolean | undefined,
TConfig extends Config<any, any> = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
constructor() {
super({ type: 'boolean' });

Expand Down Expand Up @@ -73,7 +73,7 @@ create.prototype = BooleanSchema.prototype;
export default interface BooleanSchema<
TType extends Maybe<boolean>,
TConfig extends Config<any, any> = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
): BooleanSchema<TType, ToggleDefault<TConfig, D>>;
Expand Down
4 changes: 2 additions & 2 deletions src/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function create() {
export default class DateSchema<
TType extends Maybe<Date> = Date | undefined,
TConfig extends Config<any, any> = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
static INVALID_DATE = invalidDate;

constructor() {
Expand Down Expand Up @@ -106,7 +106,7 @@ create.INVALID_DATE = invalidDate;
export default interface DateSchema<
TType extends Maybe<Date>,
TConfig extends Config<any, any> = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
): DateSchema<TType, ToggleDefault<TConfig, D>>;
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ type ObjectSchemaOf<T extends AnyObject> = ObjectSchema<
: T[k] extends AnyObject
? // we can't use ObjectSchema<{ []: SchemaOf<T[k]> }> b/c TS produces a union of two schema
ObjectSchemaOf<T[k]>
: BaseSchema<T[k], T[k], Config>;
: BaseSchema<T[k], Config>;
}
>;

type SchemaOf<T> = T extends Array<infer E>
? ArraySchema<SchemaOf<E>>
: T extends AnyObject
? ObjectSchemaOf<T>
: BaseSchema<T, T, Config>;
: BaseSchema<T, Config>;

export type AnyObjectSchema = ObjectSchema<any, any, any>;

Expand Down
4 changes: 2 additions & 2 deletions src/mixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import BaseSchema from './schema';
export declare class MixedSchema<
TType = any,
TConfig extends Config<any, any> = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
): MixedSchema<TType, ToggleDefault<TConfig, D>>;
Expand All @@ -14,7 +14,7 @@ export declare class MixedSchema<
schema: MixedSchema<IT, IC>,
): MixedSchema<NonNullable<TType> | IT, TConfig & IC>;
concat<IT, IC extends Config<any, any>>(
schema: BaseSchema<IT, any, IC>,
schema: BaseSchema<IT, IC>,
): MixedSchema<NonNullable<TType> | Optionals<IT>, TConfig & IC>;
concat(schema: this): this;

Expand Down
4 changes: 2 additions & 2 deletions src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function create() {
export default class NumberSchema<
TType extends Maybe<number> = number | undefined,
TConfig extends Config<any, any> = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
constructor() {
super({ type: 'number' });

Expand Down Expand Up @@ -151,7 +151,7 @@ create.prototype = NumberSchema.prototype;
export default interface NumberSchema<
TType extends Maybe<number> = number | undefined,
TConfig extends Config<any, any> = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
strip(): NumberSchema<TType, SetFlag<TConfig, 's'>>;

default<D extends Maybe<TType>>(
Expand Down
19 changes: 9 additions & 10 deletions src/object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import snakeCase from 'lodash/snakeCase';
import camelCase from 'lodash/camelCase';
import { camelCase, snakeCase } from 'tiny-case';
import { getter } from 'property-expr';

import { object as locale } from './locale';
Expand Down Expand Up @@ -75,7 +74,7 @@ export type AssertsShape<S extends ObjectShape> = MakePartial<
{
[K in keyof S]: FieldType<S[K], '__outputType'>;
}
>;
> & { [k: string]: any };

export type PartialSchema<S extends ObjectShape> = {
[K in keyof S]: S[K] extends BaseSchema ? ReturnType<S[K]['optional']> : S[K];
Expand Down Expand Up @@ -106,8 +105,8 @@ const defaultSort = sortByKeyOrder([]);
export default class ObjectSchema<
TShape extends ObjectShape,
TConfig extends Config<any, any> = Config<AnyObject, 'd'>,
TIn extends Maybe<TypeOfShape<TShape>> = TypeOfShape<TShape> | undefined
> extends BaseSchema<TIn, AssertsShape<TShape> | Optionals<TIn>, TConfig> {
TIn extends Maybe<AssertsShape<TShape>> = AssertsShape<TShape> | undefined
> extends BaseSchema<TIn, TConfig> {
fields: TShape = Object.create(null);

spec!: ObjectSchemaSpec;
Expand Down Expand Up @@ -316,7 +315,7 @@ export default class ObjectSchema<
? ObjectSchema<
TShape & S,
TConfig & C,
TypeOfShape<TShape & S> | Optionals<IType>
AssertsShape<TShape & S> | Optionals<IType>
>
: never;
concat(schema: this): this;
Expand Down Expand Up @@ -363,7 +362,7 @@ export default class ObjectSchema<
private setFields<S extends ObjectShape>(
shape: S,
excludedEdges?: readonly string[],
): ObjectSchema<S, TConfig, TypeOfShape<S> | Optionals<TIn>> {
): ObjectSchema<S, TConfig, AssertsShape<S> | Optionals<TIn>> {
let next = this.clone() as any;
next.fields = shape;

Expand Down Expand Up @@ -406,7 +405,7 @@ export default class ObjectSchema<
deepPartial(): ObjectSchema<
DeepPartialSchema<TShape>,
TConfig,
Optionals<TIn> | undefined | TypeOfShape<DeepPartialSchema<TShape>>
Optionals<TIn> | undefined | AssertsShape<DeepPartialSchema<TShape>>
> {
const partial: any = {};
for (const [key, schema] of Object.entries(this.fields)) {
Expand Down Expand Up @@ -533,8 +532,8 @@ create.prototype = ObjectSchema.prototype;
export default interface ObjectSchema<
TShape extends ObjectShape,
TConfig extends Config<any, any> = Config<AnyObject, 'd'>,
TIn extends Maybe<TypeOfShape<TShape>> = TypeOfShape<TShape> | undefined
> extends BaseSchema<TIn, AssertsShape<TShape> | Optionals<TIn>, TConfig> {
TIn extends Maybe<AssertsShape<TShape>> = AssertsShape<TShape> | undefined
> extends BaseSchema<TIn, TConfig> {
default<D extends Maybe<AnyObject>>(
def: Thunk<D>,
): ObjectSchema<TShape, ToggleDefault<TConfig, D>, TIn>;
Expand Down
21 changes: 9 additions & 12 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { Config, Defined, Flags, SetFlag, Thunk, _ } from './util/types';

export { Config };

export type ConfigOf<T> = T extends AnySchema<any, any, infer C> ? C : never;
export type ConfigOf<T> = T extends AnySchema<any, infer C> ? C : never;

export type ContextOf<T> = ConfigOf<T>['context'];

Expand Down Expand Up @@ -61,11 +61,10 @@ export type SchemaOptions<TDefault> = {
spec?: SchemaSpec<TDefault>;
};

export type AnySchema<
TType = any,
TOut = any,
C extends Config = any
> = BaseSchema<TType, TOut, C>;
export type AnySchema<TType = any, C extends Config = any> = BaseSchema<
TType,
C
>;

export interface CastOptions<C = {}> {
parent?: any;
Expand Down Expand Up @@ -122,15 +121,14 @@ export interface SchemaDescription {

export default abstract class BaseSchema<
TType = any,
TOut = TType,
TConfig extends Config<any, any> = Config
> {
readonly type: string;

readonly __type!: TType;
readonly __outputType!: ResolveFlags<TOut, TConfig['flags']>;
readonly __flags!: TConfig['flags'];
readonly __outputType!: ResolveFlags<TType, TConfig['flags']>;

readonly __flags!: TConfig['flags'];
readonly __isYupSchema__!: boolean;

readonly deps: readonly string[] = [];
Expand Down Expand Up @@ -803,7 +801,7 @@ export default abstract class BaseSchema<
return next;
}

strip(strip = true): BaseSchema<TType, TOut, SetFlag<TConfig, 's'>> {
strip(strip = true): BaseSchema<TType, SetFlag<TConfig, 's'>> {
let next = this.clone();
next.spec.strip = strip;
return next as any;
Expand Down Expand Up @@ -837,9 +835,8 @@ export default abstract class BaseSchema<
}

export default interface BaseSchema<
TType = any,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TOut = TType,
TType = any,
TConfig extends Config<any, any> = Config
> {
validateAt(
Expand Down
4 changes: 2 additions & 2 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export { create };
export default class StringSchema<
TType extends Maybe<string> = string | undefined,
TConfig extends AnyConfig = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
constructor() {
super({ type: 'string' });

Expand Down Expand Up @@ -219,7 +219,7 @@ create.prototype = StringSchema.prototype;
export default interface StringSchema<
TType extends Maybe<string> = string | undefined,
TConfig extends AnyConfig = Config
> extends BaseSchema<TType, TType, TConfig> {
> extends BaseSchema<TType, TConfig> {
default<D extends Maybe<TType>>(
def: Thunk<D>,
): StringSchema<TType, ToggleDefault<TConfig, D>>;
Expand Down
17 changes: 6 additions & 11 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1878,11 +1878,6 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash@^4.14.165":
version "4.14.165"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.165.tgz#74d55d947452e2de0742bad65270433b63a8c30f"
integrity sha512-tjSSOTHhI5mCHTy/OOXYIhi2Wt1qcbHmuXD1Ha7q70CgI/I71afO4XtLb/cVexki1oVYchpul/TOuu3Arcdxrg==

"@types/minimatch@*":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down Expand Up @@ -7571,11 +7566,6 @@ locate-path@^5.0.0:
dependencies:
p-locate "^4.1.0"

lodash-es@^4.17.11:
version "4.17.15"
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.15.tgz#21bd96839354412f23d7a10340e5eac6ee455d78"
integrity sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==

lodash._reinterpolate@~3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d"
Expand Down Expand Up @@ -7609,7 +7599,7 @@ lodash.templatesettings@^4.0.0:
dependencies:
lodash._reinterpolate "~3.0.0"

lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.4, lodash@^4.2.1:
lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.4, lodash@^4.2.1:
version "4.17.20"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==
Expand Down Expand Up @@ -10889,6 +10879,11 @@ timers-browserify@^2.0.4:
dependencies:
setimmediate "^1.0.4"

tiny-case@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/tiny-case/-/tiny-case-1.0.1.tgz#06b6257c0a35c72e455e23f55bab7abfc8049d8a"
integrity sha512-4d4i5qze9WwU+eVXygm+naw4Dx1GQGvISKyFAdIriw5P08+YASWCgi1+iRhC3H32vv20c7r2NP4g6lalG9Uspw==

title-case@^2.1.0:
version "2.1.1"
resolved "https://registry.yarnpkg.com/title-case/-/title-case-2.1.1.tgz#3e127216da58d2bc5becf137ab91dae3a7cd8faa"
Expand Down

0 comments on commit 0bf9732

Please sign in to comment.