Skip to content

Commit

Permalink
feat: adding Schema.uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
yUnreal committed May 21, 2024
1 parent 4163d9d commit 3c69f74
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 33 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/structs/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Model<S extends Schema<AnyObject>> {
return this.options.schema;
}

protected get driver() {
public get driver() {
return (
this.options.driver ??
new JSONBasedDriver<Infer<S>>(`./wuue/${this.name}.json`)
Expand Down
5 changes: 5 additions & 0 deletions src/structs/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BigIntSchemaKey } from './schema/BigIntSchemaKey';
import { DateSchemaKey } from './schema/DateSchemaKey';
import { MapSchemaKey } from './schema/MapSchemaKey';
import { RegexSchemaKey } from './schema/RegexSchemaKey';
import { UUIDSchemaKey } from './schema/UUIDSchemaKey';

export class Schema<Shape extends AnyObject> {
public constructor(
Expand Down Expand Up @@ -104,4 +105,8 @@ export class Schema<Shape extends AnyObject> {
public static pattern() {
return Schema.regex();
}

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

export class UUIDSchemaKey extends BaseSchemaKey<SchemaTypes.UUID> {}
45 changes: 26 additions & 19 deletions src/typings/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ 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';
import { UUIDSchemaKey } from '../structs/schema/UUIDSchemaKey';
import { AnyObject, UUID } from './utils';

export enum SchemaTypes {
String = 1,
Expand All @@ -22,6 +23,7 @@ export enum SchemaTypes {
Map,
Json,
RegExp,
UUID,
}

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

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

export type ObjectShape = Record<string, AnySchemaKey>;
Expand All @@ -92,28 +96,31 @@ export type AnySchemaKey =
| BigIntSchemaKey
| DateSchemaKey
| MapSchemaKey<AnySchemaKey, AnySchemaKey>
| RegexSchemaKey;
| RegexSchemaKey
| UUIDSchemaKey;

export type InferType<S> = S extends MappedSchemaTypes[SchemaTypes]
? 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
: S extends UUID
? SchemaTypes.UUID
: 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
6 changes: 6 additions & 0 deletions src/typings/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ export type IsExact<T, O> = [O] extends [T]
? true
: false
: false;

export type UUID = `${string}-${string}-${string}-${string}-${string}`;

export type IsAny<T> = boolean extends (T extends never ? true : false)
? true
: false;
28 changes: 17 additions & 11 deletions src/utils/getTypeOf.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable indent */
import isPlainObject from 'is-plain-obj';
import { SchemaTypes } from '../typings/schema';

Expand All @@ -7,17 +8,22 @@ export const getTypeOf = (value: unknown) => {
if (value instanceof Date) return SchemaTypes.Date;
if (Array.isArray(value)) return SchemaTypes.Array;
if (isPlainObject(value)) return SchemaTypes.Object;


const UUID_V4_PATTERN =
/^[0-9a-f]{8}-[0-9a-f]{4}-[4][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

switch (typeof value) {
case 'string':
return SchemaTypes.String;
case 'number':
return SchemaTypes.Number;
case 'bigint':
return SchemaTypes.BigInt;
case 'boolean':
return SchemaTypes.Boolean;
default:
throw new Error(`Could not get type from "${value}"`);
case 'string':
if (UUID_V4_PATTERN.test(value)) return SchemaTypes.UUID;

return SchemaTypes.String;
case 'number':
return SchemaTypes.Number;
case 'bigint':
return SchemaTypes.BigInt;
case 'boolean':
return SchemaTypes.Boolean;
default:
throw new Error(`Could not get type from "${value}"`);
}
};

0 comments on commit 3c69f74

Please sign in to comment.