Skip to content

Commit

Permalink
feat: adding support to regexs πŸŽ‰πŸŽ‰
Browse files Browse the repository at this point in the history
  • Loading branch information
yUnreal committed May 20, 2024
1 parent fac3ab8 commit 97d816f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/structs/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ObjectSchemaKey } from './schema/ObjectSchemaKey';
import { BigIntSchemaKey } from './schema/BigIntSchemaKey';
import { DateSchemaKey } from './schema/DateSchemaKey';
import { MapSchemaKey } from './schema/MapSchemaKey';
import { RegexSchemaKey } from './schema/RegexSchemaKey';

export class Schema<Shape extends AnyObject> {
public constructor(
Expand Down Expand Up @@ -95,4 +96,8 @@ export class Schema<Shape extends AnyObject> {
) {
return new MapSchemaKey(key, value, { type: SchemaTypes.Map });
}

public static regex() {
return new RegexSchemaKey({ type: SchemaTypes.RegExp });
}
}
11 changes: 11 additions & 0 deletions src/structs/schema/RegexSchemaKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SchemaTypes } from '../../typings/schema';
import { BaseSchemaKey } from './BaseSchemaKey';

export class RegexSchemaKey extends BaseSchemaKey<SchemaTypes.RegExp> {
public match(regex: RegExp, message?: string) {
return this.effect(
(crrRegex) => crrRegex.source === regex.source,
message
);
}
}
43 changes: 25 additions & 18 deletions src/typings/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DateSchemaKey } from '../structs/schema/DateSchemaKey';
import { MapSchemaKey } from '../structs/schema/MapSchemaKey';
import { NumberSchemaKey } from '../structs/schema/NumberSchemaKey';
import { ObjectSchemaKey } from '../structs/schema/ObjectSchemaKey';
import { RegexSchemaKey } from '../structs/schema/RegexSchemaKey';
import { StringSchemaKey } from '../structs/schema/StringSchemaKey';
import { AnyObject } from './utils';

Expand All @@ -20,6 +21,7 @@ export enum SchemaTypes {
Date,
Map,
Json,
RegExp,
}

export type JSONValue =
Expand All @@ -41,6 +43,7 @@ export interface MappedSchemaTypes {
[SchemaTypes.Date]: Date;
[SchemaTypes.Map]: Map<string, unknown>;
[SchemaTypes.Json]: JSONValue;
[SchemaTypes.RegExp]: RegExp;
}

export interface SchemaKeyOptions<Type extends SchemaTypes> {
Expand Down Expand Up @@ -75,6 +78,7 @@ export interface MappedSchemaKeys {
[SchemaTypes.BigInt]: BigIntSchemaKey;
[SchemaTypes.Date]: DateSchemaKey;
[SchemaTypes.Map]: MapSchemaKey<AnySchemaKey, AnySchemaKey>;
[SchemaTypes.RegExp]: RegexSchemaKey;
}

export type ObjectShape = Record<string, AnySchemaKey>;
Expand All @@ -87,26 +91,29 @@ export type AnySchemaKey =
| ObjectSchemaKey<ObjectShape>
| BigIntSchemaKey
| DateSchemaKey
| MapSchemaKey<AnySchemaKey, AnySchemaKey>;
| MapSchemaKey<AnySchemaKey, AnySchemaKey>
| RegexSchemaKey;

export type InferType<S> = S extends MappedSchemaTypes[SchemaTypes]
? S extends boolean
? SchemaTypes.Boolean
: S extends string
? SchemaTypes.String
: S extends Date
? SchemaTypes.Date
: S extends Map<string, any>
? SchemaTypes.Map
: S extends number
? SchemaTypes.Number
: S extends unknown[]
? SchemaTypes.Array
: S extends AnyObject
? SchemaTypes.Object
: S extends bigint
? SchemaTypes.BigInt
: never
? S extends RegExp
? SchemaTypes.RegExp
: S extends boolean
? SchemaTypes.Boolean
: S extends string
? SchemaTypes.String
: S extends Date
? SchemaTypes.Date
: S extends Map<string, any>
? SchemaTypes.Map
: S extends number
? SchemaTypes.Number
: S extends unknown[]
? SchemaTypes.Array
: S extends AnyObject
? SchemaTypes.Object
: S extends bigint
? SchemaTypes.BigInt
: never
: never;

export type Infer<S extends Schema<AnyObject>> = S extends Schema<infer T>
Expand Down
1 change: 1 addition & 0 deletions src/utils/getTypeOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import isPlainObject from 'is-plain-obj';
import { SchemaTypes } from '../typings/schema';

export const getTypeOf = (value: unknown) => {
if (value instanceof RegExp) return SchemaTypes.RegExp;
if (value instanceof Map) return SchemaTypes.Map;
if (value instanceof Date) return SchemaTypes.Date;
if (Array.isArray(value)) return SchemaTypes.Array;
Expand Down

0 comments on commit 97d816f

Please sign in to comment.