Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug bash #1483

Merged
merged 3 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = (api) => ({
targets: { node: 'current' },
},
],
'@babel/preset-typescript',
['@babel/preset-typescript', { allowDeclareFields: true }],
],
plugins: [
'@babel/plugin-proposal-logical-assignment-operators',
Expand Down
4 changes: 4 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"env": {
"browser": true
},
"rules": {
"@typescript-eslint/no-shadow": "off",
"@typescript-eslint/no-empty-interface": "off"
},
"overrides": [
{
"files": ["test/**"],
Expand Down
57 changes: 29 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,50 +66,51 @@
]
},
"devDependencies": {
"@4c/cli": "^2.1.12",
"@4c/rollout": "^2.1.11",
"@4c/tsconfig": "^0.3.1",
"@babel/cli": "7.12.1",
"@babel/core": "7.12.3",
"@babel/plugin-proposal-logical-assignment-operators": "^7.12.1",
"@babel/preset-typescript": "^7.12.1",
"@typescript-eslint/parser": "^4.8.1",
"@4c/cli": "^3.0.1",
"@4c/rollout": "^3.0.1",
"@4c/tsconfig": "^0.4.0",
"@babel/cli": "7.15.7",
"@babel/core": "7.15.8",
"@babel/plugin-proposal-logical-assignment-operators": "^7.14.5",
"@babel/preset-typescript": "^7.15.0",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"babel-core": "^7.0.0-bridge.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^26.6.1",
"babel-jest": "^27.2.5",
"babel-plugin-transform-rename-import": "^2.3.0",
"babel-preset-jason": "^6.3.0",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"doctoc": "^1.4.0",
"doctoc": "^2.0.1",
"eslint": "^7.12.0",
"eslint-config-jason": "^7.0.1",
"eslint-config-prettier": "^6.14.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.1.0",
"eslint-plugin-react": "^7.21.5",
"eslint-config-jason": "^8.1.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jest": "^25.0.1",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-ts-expect": "^1.0.1",
"eslint-plugin-ts-expect": "^2.0.0",
"eslint-plugin-typescript": "^0.14.0",
"husky": "^5.1.1",
"jest": "^26.6.1",
"lint-staged": "^11.0.0",
"prettier": "^2.1.2",
"rollup": "^2.32.1",
"hookem": "^2.0.1",
"jest": "^27.2.5",
"lint-staged": "^11.2.3",
"prettier": "^2.4.1",
"rollup": "^2.58.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-filesize": "^8.0.2",
"rollup-plugin-filesize": "^9.1.1",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-size-snapshot": "^0.12.0",
"sinon": "^9.2.0",
"sinon-chai": "^3.5.0",
"sinon": "^11.1.2",
"sinon-chai": "^3.7.0",
"synchronous-promise": "^2.0.15",
"typescript": "^4.0.5"
"typescript": "^4.4.3"
},
"dependencies": {
"@babel/runtime": "^7.10.5",
"@types/lodash": "^4.14.165",
"@babel/runtime": "^7.15.4",
"@types/lodash": "^4.14.175",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"nanoclone": "^0.2.1",
Expand Down
3 changes: 1 addition & 2 deletions src/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import MixedSchema from './mixed';
// @ts-ignore
import isoParse from './util/isodate';
import { date as locale, MixedLocale } from './locale';
Expand Down Expand Up @@ -77,7 +76,7 @@ export default class DateSchema<
}

max(max: unknown | Ref, message = locale.max) {
var limit = this.prepareParam(max, 'max');
let limit = this.prepareParam(max, 'max');

return this.test({
message,
Expand Down
1 change: 1 addition & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
declare module 'lodash/has' {
// eslint-disable-next-line no-inner-declarations
function has<T extends {}, Key extends PropertyKey>(
obj: T,
prop: Key,
Expand Down
4 changes: 2 additions & 2 deletions src/number.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MixedLocale, number as locale } from './locale';
import isAbsent from './util/isAbsent';
import type { AnyObject, Maybe, Message } from './types';
import type { AnyObject, Maybe } from './types';
import type Reference from './Reference';
import type { Defined, If, Thunk } from './util/types';
import BaseSchema from './schema';
Expand Down Expand Up @@ -112,7 +112,7 @@ export default class NumberSchema<
}

round(method: 'ceil' | 'floor' | 'round' | 'trunc') {
var avail = ['ceil', 'floor', 'round', 'trunc'];
let avail = ['ceil', 'floor', 'round', 'trunc'];
method = (method?.toLowerCase() as any) || ('round' as const);

// this exists for symemtry with the new Math.trunc
Expand Down
23 changes: 11 additions & 12 deletions src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import BaseSchema, {

export type Assign<T extends {}, U extends {}> = {
[P in keyof T]: P extends keyof U ? U[P] : T[P];
} &
U;
} & U;

export type AnyObject = Record<string, any>;

Expand Down Expand Up @@ -78,15 +77,16 @@ export default class ObjectSchema<
TIn extends Maybe<TypeOfShape<TShape>> = TypeOfShape<TShape>,
TOut extends Maybe<AssertsShape<TShape>> =
| AssertsShape<TShape>
| Optionals<TIn>
| Optionals<TIn>,
> extends BaseSchema<TIn, TContext, TOut> {
fields: TShape = Object.create(null);

spec!: ObjectSchemaSpec;
declare spec: ObjectSchemaSpec;

private _sortErrors = defaultSort;
private _nodes: readonly string[] = [];
private _excludedEdges: readonly string[] = [];

private _excludedEdges: readonly [nodeA: string, nodeB: string][] = [];

constructor(spec?: TShape) {
super({
Expand Down Expand Up @@ -305,7 +305,7 @@ export default class ObjectSchema<
}
}

return next.withMutation(() => next.shape(nextFields));
return next.withMutation(() => next.shape(nextFields, this._excludedEdges));
}

getDefaultFromShape(): DefaultFromShape<TShape> {
Expand Down Expand Up @@ -344,11 +344,10 @@ export default class ObjectSchema<
next._sortErrors = sortByKeyOrder(Object.keys(fields));

if (excludes.length) {
// this is a convenience for when users only supply a single pair
if (!Array.isArray(excludes[0])) excludes = [excludes as any];

let keys = excludes.map(([first, second]) => `${first}-${second}`);

next._excludedEdges = next._excludedEdges.concat(keys);
next._excludedEdges = [...next._excludedEdges, ...excludes];
}

next._nodes = sortFields(fields, next._excludedEdges);
Expand Down Expand Up @@ -458,7 +457,7 @@ create.prototype = ObjectSchema.prototype;
export interface OptionalObjectSchema<
TShape extends ObjectShape,
TContext extends AnyObject = AnyObject,
TIn extends Maybe<TypeOfShape<TShape>> = TypeOfShape<TShape>
TIn extends Maybe<TypeOfShape<TShape>> = TypeOfShape<TShape>,
> extends ObjectSchema<TShape, TContext, TIn> {
default<TNextDefault extends Maybe<AnyObject>>(
def: TNextDefault | (() => TNextDefault),
Expand Down Expand Up @@ -500,7 +499,7 @@ export interface OptionalObjectSchema<
export interface DefinedObjectSchema<
TShape extends ObjectShape,
TContext extends AnyObject,
TIn extends Maybe<TypeOfShape<TShape>>
TIn extends Maybe<TypeOfShape<TShape>>,
> extends ObjectSchema<
TShape,
TContext,
Expand Down Expand Up @@ -546,7 +545,7 @@ export interface DefinedObjectSchema<
export interface RequiredObjectSchema<
TShape extends ObjectShape,
TContext extends AnyObject,
TIn extends Maybe<TypeOfShape<TShape>>
TIn extends Maybe<TypeOfShape<TShape>>,
> extends ObjectSchema<TShape, TContext, TIn, AssertsShape<TShape>> {
default<TNextDefault extends Maybe<AnyObject>>(
def: TNextDefault | (() => TNextDefault),
Expand Down
57 changes: 29 additions & 28 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export interface SchemaDescription {
export default abstract class BaseSchema<
TCast = any,
TContext = AnyObject,
TOutput = any
TOutput = any,
> {
readonly type: string;

Expand Down Expand Up @@ -183,7 +183,7 @@ export default abstract class BaseSchema<
}

label(label: string) {
var next = this.clone();
let next = this.clone();
next.spec.label = label;
return next;
}
Expand Down Expand Up @@ -264,6 +264,7 @@ export default abstract class BaseSchema<
});
});

combined.transforms = [...base.transforms, ...combined.transforms];
return combined as any;
}

Expand Down Expand Up @@ -382,9 +383,9 @@ export default abstract class BaseSchema<

if (this._typeError) initialTests.push(this._typeError);

let finalTests = [];
let finalTests: any[] = [];
if (this._whitelistError) finalTests.push(this._whitelistError);
if (this._blacklistError) finalTests.push(this._blacklistError);
if (this._blacklistError) finalTests.push(this._blacklistError);

runTests(
{
Expand Down Expand Up @@ -500,7 +501,7 @@ export default abstract class BaseSchema<
}

strict(isStrict = true) {
var next = this.clone();
let next = this.clone();
next.spec.strict = isStrict;
return next;
}
Expand Down Expand Up @@ -534,23 +535,23 @@ export default abstract class BaseSchema<
}

notRequired(): any {
var next = this.clone({ presence: 'optional' });
let next = this.clone({ presence: 'optional' });
next.tests = next.tests.filter((test) => test.OPTIONS.name !== 'required');
return next as any;
}

nullable(isNullable?: true): any;
nullable(isNullable: false): any;
nullable(isNullable = true): any {
var next = this.clone({
let next = this.clone({
nullable: isNullable !== false,
});

return next as any;
}

transform(fn: TransformFunction<this>) {
var next = this.clone();
let next = this.clone();
next.transforms.push(fn as TransformFunction<any>);
return next;
}
Expand Down Expand Up @@ -649,7 +650,7 @@ export default abstract class BaseSchema<
}

typeError(message: Message) {
var next = this.clone();
let next = this.clone();

next._typeError = createValidation({
message,
Expand All @@ -671,7 +672,7 @@ export default abstract class BaseSchema<
enums: Array<Maybe<U> | Reference>,
message = locale.oneOf,
): this {
var next = this.clone();
let next = this.clone();

enums.forEach((val) => {
next._whitelist.add(val);
Expand All @@ -691,7 +692,7 @@ export default abstract class BaseSchema<
: this.createError({
params: {
values: valids.toArray().join(', '),
resolved
resolved,
},
});
},
Expand All @@ -704,7 +705,7 @@ export default abstract class BaseSchema<
enums: Array<Maybe<U> | Reference>,
message = locale.notOneOf,
): this {
var next = this.clone();
let next = this.clone();
enums.forEach((val) => {
next._blacklist.add(val);
next._whitelist.delete(val);
Expand All @@ -720,7 +721,7 @@ export default abstract class BaseSchema<
return this.createError({
params: {
values: invalids.toArray().join(', '),
resolved
resolved,
},
});
return true;
Expand Down Expand Up @@ -756,6 +757,7 @@ export default abstract class BaseSchema<
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default interface BaseSchema<TCast, TContext, TOutput> {
validateAt(
path: string,
Expand All @@ -778,21 +780,20 @@ export default interface BaseSchema<TCast, TContext, TOutput> {
BaseSchema.prototype.__isYupSchema__ = true;

for (const method of ['validate', 'validateSync'])
BaseSchema.prototype[
`${method}At` as 'validateAt' | 'validateSyncAt'
] = function (path: string, value: any, options: ValidateOptions = {}) {
const { parent, parentPath, schema } = getIn(
this,
path,
value,
options.context,
);
return schema[method](parent && parent[parentPath], {
...options,
parent,
path,
});
};
BaseSchema.prototype[`${method}At` as 'validateAt' | 'validateSyncAt'] =
function (path: string, value: any, options: ValidateOptions = {}) {
const { parent, parentPath, schema } = getIn(
this,
path,
value,
options.context,
);
return schema[method](parent && parent[parentPath], {
...options,
parent,
path,
});
};

for (const alias of ['equals', 'is'] as const)
BaseSchema.prototype[alias] = BaseSchema.prototype.oneOf;
Expand Down
6 changes: 4 additions & 2 deletions src/setLocale.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import locale, { LocaleObject } from './locale';

export default function setLocale(custom: LocaleObject) {
Object.keys(custom).forEach(type => {
Object.keys(custom[type]).forEach(method => {
Object.keys(custom).forEach((type) => {
// @ts-ignore
Object.keys(custom[type]).forEach((method) => {
// @ts-ignore
locale[type][method] = custom[type][method];
});
});
Expand Down
Loading