Skip to content

Commit

Permalink
feat: add get method
Browse files Browse the repository at this point in the history
  • Loading branch information
ASafaeirad committed Oct 29, 2023
1 parent c087aa7 commit e72a9ed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
44 changes: 30 additions & 14 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
import type { InferType } from './InferType';
import type { Schema, SchemaOptions } from './Schema';
import { BooleanSchema, NumberSchema, StringSchema } from './Schema';
import type { SchemaWithDefaultOptions } from './Schema/SchemaOptions';

export class Config<
TSchema extends Record<string, Schema>,
TConfig extends Record<string, unknown>,
> {
private value!: TConfig;
type RequiredSchema<T extends Schema> = T & { required: true };

export class Config<TSchema extends Record<string, Schema>> {
private value!: InferType<TSchema>;

constructor(private schema: TSchema) {}

public parse(value: TConfig) {
this.value = value;
public parse(value: Partial<Record<keyof TSchema, any>>) {
this.value = value as Record<keyof TSchema, any>;

Object.entries(this.schema).forEach(([key, s]) => {
s.key = key;
s.setValue(this.value[key]);
s.validate();
this.value[key as keyof TConfig] = s.parse();
this.value[key as keyof InferType<TSchema>] = s.parse();
});

return this;
}

static string(options?: SchemaOptions<string>) {
return new StringSchema(options);
static string<T extends SchemaOptions<string>>(
options?: T,
): T extends SchemaWithDefaultOptions<string>
? RequiredSchema<StringSchema<string>>
: StringSchema<string> {
return new StringSchema(options) as any;
}

static boolean<T extends SchemaOptions<boolean>>(
options?: T,
): T extends SchemaWithDefaultOptions<boolean>
? RequiredSchema<BooleanSchema<boolean>>
: BooleanSchema<boolean> {
return new BooleanSchema(options) as any;
}

static boolean(options?: SchemaOptions<boolean>) {
return new BooleanSchema(options);
static number<T extends SchemaOptions<number>>(
options?: T,
): T extends SchemaWithDefaultOptions<number>
? RequiredSchema<NumberSchema<number>>
: NumberSchema<number> {
return new NumberSchema(options) as any;
}

static number(options?: SchemaOptions<number>) {
return new NumberSchema(options);
public get<TKey extends keyof TSchema>(key: TKey) {
return this.value[key] as InferType<TSchema>[TKey];
}

public getAll(): InferType<TSchema> {
Expand Down
13 changes: 11 additions & 2 deletions src/Schema/SchemaOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export interface SchemaOptions<TInput> {
default?: TInput;
export interface SchemaWithDefaultOptions<TInput> {
default: TInput;
coerce?: boolean;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export interface NullableSchemaOptions<TInput> {
coerce?: boolean;
}

export type SchemaOptions<TInput> =
| NullableSchemaOptions<TInput>
| SchemaWithDefaultOptions<TInput>;

0 comments on commit e72a9ed

Please sign in to comment.