Skip to content

Commit

Permalink
fix: make default value type-safe
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Apr 13, 2024
1 parent 4fe65e2 commit 2149983
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 47 deletions.
44 changes: 0 additions & 44 deletions src/Guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,3 @@ export interface Guard<T> {
}

export class GuardError extends Error {}

export class RequiredGuard implements Guard<string> {
validate(input: unknown, field: string) {
if (input == null)
throw new GuardError(
`Invalid configuration: The "${field}" is required but the given value is "${input}"`,
);
}
}

export class TypeGuard implements Guard<any> {
constructor(private type: string) {}

validate(input: any, field: string) {
if (typeof input !== this.type)
throw new TypeError(
`Invalid configuration: The "${field}" expected to be "${
this.type
}" but a "${typeof input}" was provided`,
);
}
}

export class MinNumberGuard implements Guard<number> {
constructor(private min: number) {}

validate(input: number, field: number) {
if (input < this.min)
throw new RangeError(
`Invalid configuration: The "${field}" expected to be more than or equal to "${this.min}" but "${input}" was provided`,
);
}
}

export class MaxNumberGuard implements Guard<number> {
constructor(private max: number) {}

validate(input: number, field: number) {
if (input > this.max)
throw new RangeError(
`Invalid configuration: The "${field}" expected to be less than or equal to "${this.max}" but "${input}" was provided`,
);
}
}
26 changes: 24 additions & 2 deletions src/Schema/NumberSchema.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
import { MaxNumberGuard, MinNumberGuard } from '../Guard';
import type { Guard } from '../Guard';
import { Schema } from './Schema';
import type { SchemaOptions } from './SchemaOptions';

class MinNumberGuard implements Guard<number> {
constructor(private min: number) {}

validate(input: number, field: number) {
if (input < this.min)
throw new RangeError(
`Invalid configuration: The "${field}" expected to be more than or equal to "${this.min}" but "${input}" was provided`,
);
}
}

class MaxNumberGuard implements Guard<number> {
constructor(private max: number) {}

validate(input: number, field: number) {
if (input > this.max)
throw new RangeError(
`Invalid configuration: The "${field}" expected to be less than or equal to "${this.max}" but "${input}" was provided`,
);
}
}

export class NumberSchema<TInput = any> extends Schema<TInput, number> {
constructor(options: SchemaOptions<TInput> = {}) {
constructor(options: SchemaOptions<number> = {}) {
super({ ...options, typeConstructor: Number, type: 'number' });
}

Expand Down
2 changes: 1 addition & 1 deletion src/Schema/StringSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Schema } from './Schema';
import type { SchemaOptions } from './SchemaOptions';

export class StringSchema<TInput = any> extends Schema<TInput, string> {
constructor(options: SchemaOptions<TInput> = {}) {
constructor(options: SchemaOptions<string> = {}) {
super({ ...options, typeConstructor: String, type: 'string' });
}
}

0 comments on commit 2149983

Please sign in to comment.