Skip to content

Commit

Permalink
fix: change require to isRequired
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Apr 13, 2024
1 parent d93424f commit c458d87
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Schema, SchemaOptions } from './Schema';
import { BooleanSchema, NumberSchema, StringSchema } from './Schema';
import type { SchemaWithDefaultOptions } from './Schema/SchemaOptions';

type RequiredSchema<T extends Schema> = T & { required: true };
type RequiredSchema<T extends Schema> = T & { isRequired: true };

export class Config<TSchema extends Record<string, Schema>> {
private value!: InferType<TSchema>;
Expand Down
2 changes: 1 addition & 1 deletion src/Guard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface Guard<T> {
validate: (input: T, field: T) => void;
validate: (input: T, field: string) => void;
}

export class GuardError extends Error {}
2 changes: 1 addition & 1 deletion src/InferType.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Schema } from './Schema';

export type InferType<T extends Record<string, Schema>> = {
[K in keyof T]: T[K]['required'] extends true
[K in keyof T]: T[K]['isRequired'] extends true
? NonNullable<T[K]['value']>
: T[K]['value'];
};
4 changes: 2 additions & 2 deletions src/Schema/NumberSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { SchemaOptions } from './SchemaOptions';
class MinNumberGuard implements Guard<number> {
constructor(private min: number) {}

validate(input: number, field: number) {
validate(input: number, field: string) {
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`,
Expand All @@ -16,7 +16,7 @@ class MinNumberGuard implements Guard<number> {
class MaxNumberGuard implements Guard<number> {
constructor(private max: number) {}

validate(input: number, field: number) {
validate(input: number, field: string) {
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`,
Expand Down
4 changes: 2 additions & 2 deletions src/Schema/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Schema<TInput = any, TValue = any> {
protected guards: Guard<any>[];
public value: TValue | undefined;
public key!: string;
public required!: boolean;
public isRequired!: boolean;

constructor(public options: SchemaOptions<TInput, TValue>) {
this.options.coerce ??= true;
Expand All @@ -57,7 +57,7 @@ export class Schema<TInput = any, TValue = any> {

public require() {
this.guards.unshift(new RequiredGuard());
return this as this & { required: true };
return this as this & { isRequired: true };
}

public validate() {
Expand Down

0 comments on commit c458d87

Please sign in to comment.