Skip to content

Commit

Permalink
feat: adding default/optional feats
Browse files Browse the repository at this point in the history
  • Loading branch information
yUnreal committed May 21, 2024
1 parent 5313353 commit 93d72dd
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/structs/schema/BaseSchemaKey.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SchemaError } from '../../errors/SchemaError';
import {
MappedSchemaKeys,
MappedSchemaTypes,
Expand Down Expand Up @@ -30,7 +31,17 @@ export abstract class BaseSchemaKey<Type extends SchemaTypes> {
return this;
}

public parse(value: unknown) {
public parse(value?: unknown) {
const { default: defaultFn } = this.options;

if (value === undefined) {
if (this.options.optional) return;
if (!defaultFn)
throw new SchemaError('Invalid value while parsing', this);

value = defaultFn();
}

const { cast } = this.options;

if (cast) value = cast(value);
Expand Down

0 comments on commit 93d72dd

Please sign in to comment.