diff --git a/src/lib/Shapes.ts b/src/lib/Shapes.ts index a965e5de..e9515822 100644 --- a/src/lib/Shapes.ts +++ b/src/lib/Shapes.ts @@ -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, @@ -46,7 +46,7 @@ export class Shapes { return new DateValidator(); } - public object(shape: MappedObjectValidator) { + public object(shape: MappedObjectValidator) { return new ObjectValidator(shape); } diff --git a/src/lib/util-types.ts b/src/lib/util-types.ts index 20cff056..03b9526d 100644 --- a/src/lib/util-types.ts +++ b/src/lib/util-types.ts @@ -6,6 +6,9 @@ export type Constructor = (new (...args: readonly any[]) => T) | (abstract ne export type Type = V extends BaseValidator ? T : never; +/** + * @deprecated Use `object` instead. + */ // eslint-disable-next-line @typescript-eslint/ban-types export type NonNullObject = {} & object; @@ -21,14 +24,14 @@ export type PickUndefinedMakeOptional = { [K in keyof T as undefined extends T[K] ? K : never]+?: Exclude; }; -type FilterDefinedKeys = Exclude< +type FilterDefinedKeys = Exclude< { [TKey in keyof TObj]: undefined extends TObj[TKey] ? never : TKey; }[keyof TObj], undefined >; -export type UndefinedToOptional = Pick> & { +export type UndefinedToOptional = Pick> & { [k in keyof Omit>]?: Exclude; }; @@ -87,7 +90,7 @@ export type MappedObjectValidator = { [key in keyof T]: BaseValidator * }); * ``` */ -export type SchemaOf = ObjectValidator; +export type SchemaOf = ObjectValidator; /** * Infers the type of a schema object given `typeof schema`. diff --git a/src/validators/ObjectValidator.ts b/src/validators/ObjectValidator.ts index a4670239..d16c48a7 100644 --- a/src/validators/ObjectValidator.ts +++ b/src/validators/ObjectValidator.ts @@ -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> extends BaseValidator { +export class ObjectValidator> extends BaseValidator { public readonly shape: MappedObjectValidator; public readonly strategy: ObjectValidatorStrategy; private readonly keys: readonly (keyof I)[] = []; - private readonly handleStrategy: (value: NonNullObject) => Result; + private readonly handleStrategy: (value: object) => Result; private readonly requiredKeys = new Map>(); private readonly possiblyUndefinedKeys = new Map>(); @@ -109,7 +109,7 @@ export class ObjectValidator return Reflect.construct(this.constructor, [shape, this.strategy, this.constraints]); } - public extend(schema: ObjectValidator | MappedObjectValidator): ObjectValidator { + public extend(schema: ObjectValidator | MappedObjectValidator): ObjectValidator { const shape = { ...this.shape, ...(schema instanceof ObjectValidator ? schema.shape : schema) }; return Reflect.construct(this.constructor, [shape, this.strategy, this.constraints]); } @@ -146,20 +146,20 @@ export class ObjectValidator 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 { + private handleIgnoreStrategy(value: object): Result { 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) => { - 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]; @@ -215,13 +215,13 @@ export class ObjectValidator : Result.err(new CombinedPropertyError(errors)); } - private handleStrictStrategy(value: NonNullObject): Result { + private handleStrictStrategy(value: object): Result { 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) => { - 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]; @@ -268,7 +268,7 @@ export class ObjectValidator : Result.err(new CombinedPropertyError(errors)); } - private handlePassthroughStrategy(value: NonNullObject): Result { + private handlePassthroughStrategy(value: object): Result { const result = this.handleIgnoreStrategy(value); return result.isErr() ? result : Result.ok({ ...value, ...result.value } as I); }