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

refactor: remove NonNullObject #227

Merged
merged 1 commit into from
Dec 3, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/Shapes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TypedArray, TypedArrayName } from '../constraints/util/typedArray';
import type { NonNullObject, Unwrap, UnwrapTuple } from '../lib/util-types';
import type { Unwrap, UnwrapTuple } from '../lib/util-types';
import {
ArrayValidator,
BaseValidator,
Expand Down Expand Up @@ -46,7 +46,7 @@ export class Shapes {
return new DateValidator();
}

public object<T extends NonNullObject>(shape: MappedObjectValidator<T>) {
public object<T extends object>(shape: MappedObjectValidator<T>) {
return new ObjectValidator<T>(shape);
}

Expand Down
9 changes: 6 additions & 3 deletions src/lib/util-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export type Constructor<T> = (new (...args: readonly any[]) => T) | (abstract ne

export type Type<V> = V extends BaseValidator<infer T> ? T : never;

/**
* @deprecated Use `object` instead.
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export type NonNullObject = {} & object;

Expand All @@ -21,14 +24,14 @@ export type PickUndefinedMakeOptional<T> = {
[K in keyof T as undefined extends T[K] ? K : never]+?: Exclude<T[K], undefined>;
};

type FilterDefinedKeys<TObj extends NonNullObject> = Exclude<
type FilterDefinedKeys<TObj extends object> = Exclude<
{
[TKey in keyof TObj]: undefined extends TObj[TKey] ? never : TKey;
}[keyof TObj],
undefined
>;

export type UndefinedToOptional<T extends NonNullObject> = Pick<T, FilterDefinedKeys<T>> & {
export type UndefinedToOptional<T extends object> = Pick<T, FilterDefinedKeys<T>> & {
[k in keyof Omit<T, FilterDefinedKeys<T>>]?: Exclude<T[k], undefined>;
};

Expand Down Expand Up @@ -87,7 +90,7 @@ export type MappedObjectValidator<T> = { [key in keyof T]: BaseValidator<T[key]>
* });
* ```
*/
export type SchemaOf<T extends NonNullObject> = ObjectValidator<T>;
export type SchemaOf<T extends object> = ObjectValidator<T>;

/**
* Infers the type of a schema object given `typeof schema`.
Expand Down
20 changes: 10 additions & 10 deletions src/validators/ObjectValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { MissingPropertyError } from '../lib/errors/MissingPropertyError';
import { UnknownPropertyError } from '../lib/errors/UnknownPropertyError';
import { ValidationError } from '../lib/errors/ValidationError';
import { Result } from '../lib/Result';
import type { MappedObjectValidator, NonNullObject, UndefinedToOptional } from '../lib/util-types';
import type { MappedObjectValidator, UndefinedToOptional } from '../lib/util-types';
import { BaseValidator } from './BaseValidator';
import { DefaultValidator } from './DefaultValidator';
import { LiteralValidator } from './LiteralValidator';
import { NullishValidator } from './NullishValidator';
import { UnionValidator } from './UnionValidator';

export class ObjectValidator<T extends NonNullObject, I = UndefinedToOptional<T>> extends BaseValidator<I> {
export class ObjectValidator<T extends object, I = UndefinedToOptional<T>> extends BaseValidator<I> {
public readonly shape: MappedObjectValidator<T>;
public readonly strategy: ObjectValidatorStrategy;
private readonly keys: readonly (keyof I)[] = [];
private readonly handleStrategy: (value: NonNullObject) => Result<I, CombinedPropertyError>;
private readonly handleStrategy: (value: object) => Result<I, CombinedPropertyError>;

private readonly requiredKeys = new Map<keyof I, BaseValidator<unknown>>();
private readonly possiblyUndefinedKeys = new Map<keyof I, BaseValidator<unknown>>();
Expand Down Expand Up @@ -109,7 +109,7 @@ export class ObjectValidator<T extends NonNullObject, I = UndefinedToOptional<T>
return Reflect.construct(this.constructor, [shape, this.strategy, this.constraints]);
}

public extend<ET extends NonNullObject>(schema: ObjectValidator<ET> | MappedObjectValidator<ET>): ObjectValidator<T & ET> {
public extend<ET extends object>(schema: ObjectValidator<ET> | MappedObjectValidator<ET>): ObjectValidator<T & ET> {
const shape = { ...this.shape, ...(schema instanceof ObjectValidator ? schema.shape : schema) };
return Reflect.construct(this.constructor, [shape, this.strategy, this.constraints]);
}
Expand Down Expand Up @@ -146,20 +146,20 @@ export class ObjectValidator<T extends NonNullObject, I = UndefinedToOptional<T>
return Result.ok(value as I);
}

return this.handleStrategy(value as NonNullObject);
return this.handleStrategy(value as object);
}

protected override clone(): this {
return Reflect.construct(this.constructor, [this.shape, this.strategy, this.constraints]);
}

private handleIgnoreStrategy(value: NonNullObject): Result<I, CombinedPropertyError> {
private handleIgnoreStrategy(value: object): Result<I, CombinedPropertyError> {
const errors: [PropertyKey, BaseError][] = [];
const finalObject = {} as I;
const inputEntries = new Map(Object.entries(value) as [keyof I, unknown][]);

const runPredicate = (key: keyof I, predicate: BaseValidator<unknown>) => {
const result = predicate.run(value[key as keyof NonNullObject]);
const result = predicate.run(value[key as keyof object]);

if (result.isOk()) {
finalObject[key] = result.value as I[keyof I];
Expand Down Expand Up @@ -215,13 +215,13 @@ export class ObjectValidator<T extends NonNullObject, I = UndefinedToOptional<T>
: Result.err(new CombinedPropertyError(errors));
}

private handleStrictStrategy(value: NonNullObject): Result<I, CombinedPropertyError> {
private handleStrictStrategy(value: object): Result<I, CombinedPropertyError> {
const errors: [PropertyKey, BaseError][] = [];
const finalResult = {} as I;
const inputEntries = new Map(Object.entries(value) as [keyof I, unknown][]);

const runPredicate = (key: keyof I, predicate: BaseValidator<unknown>) => {
const result = predicate.run(value[key as keyof NonNullObject]);
const result = predicate.run(value[key as keyof object]);

if (result.isOk()) {
finalResult[key] = result.value as I[keyof I];
Expand Down Expand Up @@ -268,7 +268,7 @@ export class ObjectValidator<T extends NonNullObject, I = UndefinedToOptional<T>
: Result.err(new CombinedPropertyError(errors));
}

private handlePassthroughStrategy(value: NonNullObject): Result<I, CombinedPropertyError> {
private handlePassthroughStrategy(value: object): Result<I, CombinedPropertyError> {
const result = this.handleIgnoreStrategy(value);
return result.isErr() ? result : Result.ok({ ...value, ...result.value } as I);
}
Expand Down