Skip to content

Commit

Permalink
feat: adding support to bigints
Browse files Browse the repository at this point in the history
  • Loading branch information
yUnreal committed May 25, 2024
1 parent ada7e01 commit 5eb7923
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 31 deletions.
4 changes: 4 additions & 0 deletions lib/structs/schema/BigIntSchemaKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Types } from '../../types/schema';
import { NumberBasedSchemaKey } from './NumberBasedSchemaKey';

export class BigIntSchemaKey extends NumberBasedSchemaKey<Types.BigInt> {}
34 changes: 34 additions & 0 deletions lib/structs/schema/NumberBasedSchemaKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Expression, Types } from '../../types/schema';
import { SchemaKey } from './SchemaKey';

export abstract class NumberBasedSchemaKey<
T extends Types.Number | Types.BigInt,
> extends SchemaKey<T> {
public min(
number: number,
message = `Min length of the "${Expression.Key}" must be ${number}`
) {
return this.effect((num) => num > number, message);
}

public max(
number: number,
message = `Max length of the "${Expression.Key}" must be ${number}`
) {
return this.effect((num) => num < number, message);
}

public gte(
number: number,
message = `"${Expression.Key}" must be greater or equal than ${number}`
) {
return this.min(number - 1, message);
}

public lte(
number: number,
message = `"${Expression.Key}" must be less or equal to ${number}`
) {
return this.max(number - 1, message);
}
}
32 changes: 2 additions & 30 deletions lib/structs/schema/NumberSchemaKey.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,12 @@
import { Types, Expression } from '../../types/schema';
import { SchemaKey } from './SchemaKey';
import { NumberBasedSchemaKey } from './NumberBasedSchemaKey';

export class NumberSchemaKey extends SchemaKey<Types.Number> {
export class NumberSchemaKey extends NumberBasedSchemaKey<Types.Number> {
public integer(message = `"${Expression.Key}" must be a integer`) {
return this.effect(Number.isSafeInteger, message);
}

public float(message = `"${Expression.Key}" me be a float`) {
return this.effect((num) => !Number.isInteger(num), message);
}

public min(
number: number,
message = `Min length of the "${Expression.Key}" must be ${number}`
) {
return this.effect((num) => num > number, message);
}

public max(
number: number,
message = `Max length of the "${Expression.Key}" must be ${number}`
) {
return this.effect((num) => num < number, message);
}

public gte(
number: number,
message = `"${Expression.Key}" must be greater or equal than ${number}`
) {
return this.min(number - 1, message);
}

public lte(
number: number,
message = `"${Expression.Key}" must be less or equal to ${number}`
) {
return this.max(number - 1, message);
}
}
5 changes: 5 additions & 0 deletions lib/structs/schema/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InferShape, SchemaKeyFlags, Types } from '../../types/schema';
import { StringSchemaKey } from './StringSchemaKey';
import { NumberSchemaKey } from './NumberSchemaKey';
import { BooleanSchemaKey } from './BooleanSchemaKey';
import { BigIntSchemaKey } from './BigIntSchemaKey';

export class Schema<S extends AnyObject> {
public constructor(public shape: Required<InferShape<S>>) {}
Expand All @@ -20,6 +21,10 @@ export class Schema<S extends AnyObject> {
return new BooleanSchemaKey({ type: Types.Boolean, flags: [] });
}

public static bigint() {
return new BigIntSchemaKey({ type: Types.BigInt, flags: [] });
}

public parse<O>(object: O) {
if (!isPlainObject(object))
throw new Error('Invalid value when parsing schema');
Expand Down
8 changes: 7 additions & 1 deletion lib/types/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BigIntSchemaKey } from '../structs/schema/BigIntSchemaKey';
import { BooleanSchemaKey } from '../structs/schema/BooleanSchemaKey';
import { NumberSchemaKey } from '../structs/schema/NumberSchemaKey';
import { Schema } from '../structs/schema/Schema';
Expand All @@ -8,6 +9,7 @@ export enum Types {
String,
Number,
Boolean,
BigInt,
}

export type SchemaDefinition = {
Expand All @@ -28,12 +30,14 @@ export interface MappedSchemaTypes {
[Types.String]: string;
[Types.Number]: number;
[Types.Boolean]: boolean;
[Types.BigInt]: bigint;
}

export interface MappedSchemaKeys {
[Types.String]: StringSchemaKey;
[Types.Number]: NumberSchemaKey;
[Types.Boolean]: BooleanSchemaKey;
[Types.BigInt]: BigIntSchemaKey;
}

export type InferShape<S extends AnyObject> = {
Expand All @@ -46,7 +50,9 @@ export type ExtractType<S> = S extends string
? Types.Number
: S extends boolean
? Types.Boolean
: never;
: S extends bigint
? Types.BigInt
: never;

export interface Constraint<Type extends Types> {
effect(value: MappedSchemaTypes[Type]): unknown;
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/getType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const getType = (value: unknown) => {
return Types.String;
case 'boolean':
return Types.Boolean;
case 'bigint':
return Types.BigInt;
default:
throw new Error('Could not get the type from the value');
}
Expand Down

0 comments on commit 5eb7923

Please sign in to comment.