Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/goofy-dogs-knock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typestone': minor
---

Adds InstanceCheck to enable validating class instances
53 changes: 53 additions & 0 deletions packages/typestone/src/checks/instance/def.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
type ErrorHandlerParameter,
type ErrorMap,
errorParamToErrorMap,
} from '../../error/error.ts';
import { processIssue } from '../../internal/process/process-issue.ts';
import { type CheckDef } from '../check/check.ts';
import { type InstanceCheck } from './instance.ts';
import { type AnyClass } from './types.ts';
import { getConstructorName } from './utils.ts';

export type InstanceErrorMap = ErrorMap<'invalid_type'>;

export interface InstanceDef<TClass extends AnyClass> extends CheckDef<
InstanceType<TClass>,
InstanceType<TClass>,
InstanceErrorMap
> {
readonly type: 'instance';

readonly ctor: TClass;
}

export function instanceDef<const TClass extends AnyClass>(
ctor: TClass,
error: ErrorHandlerParameter<InstanceErrorMap>,
): InstanceDef<TClass> {
return {
kind: 'check',
type: 'instance',
errorMap: errorParamToErrorMap(error),

ctor,

_process,
};
}

const _process: InstanceCheck['_process'] = function* (context) {
const ctorName = getConstructorName(this.ctor) ?? 'No constructor name';
const valueCtorName =
getConstructorName(context.value) ?? 'No constructor name';
if (!((context.value as unknown) instanceof this.ctor)) {
yield* processIssue(this.errorMap, {
code: 'invalid_type',
path: context.path,
input: context.value,
expected: ctorName,
received: valueCtorName,
message: `Expected an instance of ${ctorName}.`,
});
}
};
2 changes: 2 additions & 0 deletions packages/typestone/src/checks/instance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './instance.ts';
export * from './types.ts';
17 changes: 17 additions & 0 deletions packages/typestone/src/checks/instance/instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type ErrorHandlerParameter } from '../../error/error.ts';
import { createCheck, type DefineCheck } from '../check/check.ts';
import { type InstanceDef, instanceDef, type InstanceErrorMap } from './def.ts';
import { type AnyClass } from './types.ts';

export interface InstanceCheck<
TClass extends AnyClass = AnyClass,
> extends DefineCheck<InstanceDef<TClass>> {
kind: 'check';
}

export function instance<const TClass extends AnyClass>(
ctor: TClass,
error?: ErrorHandlerParameter<InstanceErrorMap>,
): InstanceCheck<TClass> {
return createCheck(instanceDef(ctor, error ?? {}));
}
1 change: 1 addition & 0 deletions packages/typestone/src/checks/instance/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type AnyClass = new (...args: any[]) => any;
17 changes: 17 additions & 0 deletions packages/typestone/src/checks/instance/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export function getConstructorName(value: unknown): string | null {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const isConstructor = typeof value === 'function' && value.prototype;
if (isConstructor) {
return value.name || null;
}

const isObject = typeof value === 'object' && value !== null;
if (isObject) {
const ctor = value.constructor;
if (typeof ctor === 'function') {
return ctor.name || null;
}
}

return null;
}
1 change: 1 addition & 0 deletions packages/typestone/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './checks/ends-with/index.ts';
export * from './checks/eq/index.ts';
export * from './checks/gt/index.ts';
export * from './checks/gte/index.ts';
export * from './checks/instance/index.ts';
export * from './checks/integer/index.ts';
export * from './checks/lt/index.ts';
export * from './checks/lte/index.ts';
Expand Down