From 0bf97327d406c9d982b2c0a93069bd047b53d5ef Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Thu, 17 Dec 2020 16:28:42 -0500 Subject: [PATCH] feat: remove unneeded Out type from schema --- .babelrc.js | 11 +---------- package.json | 6 ++---- rollup.config.js | 8 +------- src/array.ts | 10 ++++------ src/boolean.ts | 4 ++-- src/date.ts | 4 ++-- src/index.ts | 4 ++-- src/mixed.ts | 4 ++-- src/number.ts | 4 ++-- src/object.ts | 19 +++++++++---------- src/schema.ts | 21 +++++++++------------ src/string.ts | 4 ++-- yarn.lock | 17 ++++++----------- 13 files changed, 44 insertions(+), 72 deletions(-) diff --git a/.babelrc.js b/.babelrc.js index 0368b0780..9d30fd373 100644 --- a/.babelrc.js +++ b/.babelrc.js @@ -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'], }); diff --git a/package.json b/package.json index f962f1eee..7bcbd8aad 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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": { diff --git a/rollup.config.js b/rollup.config.js index a22b70e07..4ceda37e9 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -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 = [ diff --git a/src/array.ts b/src/array.ts index e9f7738b6..3d88cf753 100644 --- a/src/array.ts +++ b/src/array.ts @@ -9,7 +9,6 @@ import type { Callback, Message, Maybe, - Optionals, } from './types'; import ValidationError from './ValidationError'; import type Reference from './Reference'; @@ -21,7 +20,6 @@ import { SetFlag, Thunk, ToggleDefault, - TypeOf, } from './util/types'; import BaseSchema, { AnySchema, @@ -43,8 +41,8 @@ export function create< export default class ArraySchema< T extends AnySchema | Lazy, C extends Config = Config, - TIn extends Maybe[]> = TypeOf[] | undefined -> extends BaseSchema[] | Optionals, C> { + TIn extends Maybe[]> = Asserts[] | undefined +> extends BaseSchema { innerType?: T; constructor(type?: T) { @@ -288,8 +286,8 @@ create.prototype = ArraySchema.prototype; export default interface ArraySchema< T extends AnySchema | Lazy, C extends Config = Config, - TIn extends Maybe[]> = TypeOf[] | undefined -> extends BaseSchema[] | Optionals, C> { + TIn extends Maybe[]> = Asserts[] | undefined +> extends BaseSchema { default>( def: Thunk, ): ArraySchema, TIn>; diff --git a/src/boolean.ts b/src/boolean.ts index 99e0a4a34..6ea0036e6 100644 --- a/src/boolean.ts +++ b/src/boolean.ts @@ -22,7 +22,7 @@ export function create() { export default class BooleanSchema< TType extends Maybe = boolean | undefined, TConfig extends Config = Config -> extends BaseSchema { +> extends BaseSchema { constructor() { super({ type: 'boolean' }); @@ -73,7 +73,7 @@ create.prototype = BooleanSchema.prototype; export default interface BooleanSchema< TType extends Maybe, TConfig extends Config = Config -> extends BaseSchema { +> extends BaseSchema { default>( def: Thunk, ): BooleanSchema>; diff --git a/src/date.ts b/src/date.ts index a05ef64fa..e56bb8a7f 100644 --- a/src/date.ts +++ b/src/date.ts @@ -30,7 +30,7 @@ export function create() { export default class DateSchema< TType extends Maybe = Date | undefined, TConfig extends Config = Config -> extends BaseSchema { +> extends BaseSchema { static INVALID_DATE = invalidDate; constructor() { @@ -106,7 +106,7 @@ create.INVALID_DATE = invalidDate; export default interface DateSchema< TType extends Maybe, TConfig extends Config = Config -> extends BaseSchema { +> extends BaseSchema { default>( def: Thunk, ): DateSchema>; diff --git a/src/index.ts b/src/index.ts index 4e43f6d7d..c1bd848c4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -43,7 +43,7 @@ type ObjectSchemaOf = ObjectSchema< : T[k] extends AnyObject ? // we can't use ObjectSchema<{ []: SchemaOf }> b/c TS produces a union of two schema ObjectSchemaOf - : BaseSchema; + : BaseSchema; } >; @@ -51,7 +51,7 @@ type SchemaOf = T extends Array ? ArraySchema> : T extends AnyObject ? ObjectSchemaOf - : BaseSchema; + : BaseSchema; export type AnyObjectSchema = ObjectSchema; diff --git a/src/mixed.ts b/src/mixed.ts index 09fc4a469..1386de9cb 100644 --- a/src/mixed.ts +++ b/src/mixed.ts @@ -5,7 +5,7 @@ import BaseSchema from './schema'; export declare class MixedSchema< TType = any, TConfig extends Config = Config -> extends BaseSchema { +> extends BaseSchema { default>( def: Thunk, ): MixedSchema>; @@ -14,7 +14,7 @@ export declare class MixedSchema< schema: MixedSchema, ): MixedSchema | IT, TConfig & IC>; concat>( - schema: BaseSchema, + schema: BaseSchema, ): MixedSchema | Optionals, TConfig & IC>; concat(schema: this): this; diff --git a/src/number.ts b/src/number.ts index bcfaecdf8..819871347 100644 --- a/src/number.ts +++ b/src/number.ts @@ -28,7 +28,7 @@ export function create() { export default class NumberSchema< TType extends Maybe = number | undefined, TConfig extends Config = Config -> extends BaseSchema { +> extends BaseSchema { constructor() { super({ type: 'number' }); @@ -151,7 +151,7 @@ create.prototype = NumberSchema.prototype; export default interface NumberSchema< TType extends Maybe = number | undefined, TConfig extends Config = Config -> extends BaseSchema { +> extends BaseSchema { strip(): NumberSchema>; default>( diff --git a/src/object.ts b/src/object.ts index f1e31f99b..07a400355 100644 --- a/src/object.ts +++ b/src/object.ts @@ -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'; @@ -75,7 +74,7 @@ export type AssertsShape = MakePartial< { [K in keyof S]: FieldType; } ->; +> & { [k: string]: any }; export type PartialSchema = { [K in keyof S]: S[K] extends BaseSchema ? ReturnType : S[K]; @@ -106,8 +105,8 @@ const defaultSort = sortByKeyOrder([]); export default class ObjectSchema< TShape extends ObjectShape, TConfig extends Config = Config, - TIn extends Maybe> = TypeOfShape | undefined -> extends BaseSchema | Optionals, TConfig> { + TIn extends Maybe> = AssertsShape | undefined +> extends BaseSchema { fields: TShape = Object.create(null); spec!: ObjectSchemaSpec; @@ -316,7 +315,7 @@ export default class ObjectSchema< ? ObjectSchema< TShape & S, TConfig & C, - TypeOfShape | Optionals + AssertsShape | Optionals > : never; concat(schema: this): this; @@ -363,7 +362,7 @@ export default class ObjectSchema< private setFields( shape: S, excludedEdges?: readonly string[], - ): ObjectSchema | Optionals> { + ): ObjectSchema | Optionals> { let next = this.clone() as any; next.fields = shape; @@ -406,7 +405,7 @@ export default class ObjectSchema< deepPartial(): ObjectSchema< DeepPartialSchema, TConfig, - Optionals | undefined | TypeOfShape> + Optionals | undefined | AssertsShape> > { const partial: any = {}; for (const [key, schema] of Object.entries(this.fields)) { @@ -533,8 +532,8 @@ create.prototype = ObjectSchema.prototype; export default interface ObjectSchema< TShape extends ObjectShape, TConfig extends Config = Config, - TIn extends Maybe> = TypeOfShape | undefined -> extends BaseSchema | Optionals, TConfig> { + TIn extends Maybe> = AssertsShape | undefined +> extends BaseSchema { default>( def: Thunk, ): ObjectSchema, TIn>; diff --git a/src/schema.ts b/src/schema.ts index 1509e6268..14459eeda 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -32,7 +32,7 @@ import { Config, Defined, Flags, SetFlag, Thunk, _ } from './util/types'; export { Config }; -export type ConfigOf = T extends AnySchema ? C : never; +export type ConfigOf = T extends AnySchema ? C : never; export type ContextOf = ConfigOf['context']; @@ -61,11 +61,10 @@ export type SchemaOptions = { spec?: SchemaSpec; }; -export type AnySchema< - TType = any, - TOut = any, - C extends Config = any -> = BaseSchema; +export type AnySchema = BaseSchema< + TType, + C +>; export interface CastOptions { parent?: any; @@ -122,15 +121,14 @@ export interface SchemaDescription { export default abstract class BaseSchema< TType = any, - TOut = TType, TConfig extends Config = Config > { readonly type: string; readonly __type!: TType; - readonly __outputType!: ResolveFlags; - readonly __flags!: TConfig['flags']; + readonly __outputType!: ResolveFlags; + readonly __flags!: TConfig['flags']; readonly __isYupSchema__!: boolean; readonly deps: readonly string[] = []; @@ -803,7 +801,7 @@ export default abstract class BaseSchema< return next; } - strip(strip = true): BaseSchema> { + strip(strip = true): BaseSchema> { let next = this.clone(); next.spec.strip = strip; return next as any; @@ -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 = Config > { validateAt( diff --git a/src/string.ts b/src/string.ts index d51824cd3..ea2eb29fb 100644 --- a/src/string.ts +++ b/src/string.ts @@ -44,7 +44,7 @@ export { create }; export default class StringSchema< TType extends Maybe = string | undefined, TConfig extends AnyConfig = Config -> extends BaseSchema { +> extends BaseSchema { constructor() { super({ type: 'string' }); @@ -219,7 +219,7 @@ create.prototype = StringSchema.prototype; export default interface StringSchema< TType extends Maybe = string | undefined, TConfig extends AnyConfig = Config -> extends BaseSchema { +> extends BaseSchema { default>( def: Thunk, ): StringSchema>; diff --git a/yarn.lock b/yarn.lock index 7eed2383b..d311a0aa3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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== @@ -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"