mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-03 08:36:01 +00:00
fixes
This commit is contained in:
@@ -26,14 +26,13 @@ export class SchemaObject<Schema extends TSchema = TSchema> {
|
||||
initial?: Partial<s.Static<Schema>>,
|
||||
private options?: SchemaObjectOptions<Schema>,
|
||||
) {
|
||||
this._default = _schema.template() as any;
|
||||
this._value = initial
|
||||
? parse(_schema, structuredClone(initial as any), {
|
||||
withDefaults: true,
|
||||
forceParse: this.isForceParse(),
|
||||
skipMark: this.isForceParse(),
|
||||
})
|
||||
: (this._default as any);
|
||||
this._default = _schema.template({}, { withOptional: true }) as any;
|
||||
this._value = parse(_schema, structuredClone(initial ?? {}), {
|
||||
withDefaults: true,
|
||||
withExtendedDefaults: true,
|
||||
forceParse: this.isForceParse(),
|
||||
skipMark: this.isForceParse(),
|
||||
});
|
||||
this._config = Object.freeze(this._value);
|
||||
}
|
||||
|
||||
|
||||
@@ -38,6 +38,7 @@ export class InvalidSchemaError extends Error {
|
||||
|
||||
export type ParseOptions = {
|
||||
withDefaults?: boolean;
|
||||
withExtendedDefaults?: boolean;
|
||||
coerce?: boolean;
|
||||
clone?: boolean;
|
||||
skipMark?: boolean; // @todo: do something with this
|
||||
@@ -57,8 +58,11 @@ export function parse<S extends s.Schema, Options extends ParseOptions = ParseOp
|
||||
): Options extends { coerce: true } ? s.StaticCoerced<S> : s.Static<S> {
|
||||
const schema = (opts?.clone ? cloneSchema(_schema as any) : _schema) as s.Schema;
|
||||
let value = opts?.coerce !== false ? schema.coerce(v) : v;
|
||||
if (opts?.withDefaults) {
|
||||
value = schema.template(value, { withOptional: true });
|
||||
if (opts?.withDefaults !== false) {
|
||||
value = schema.template(value, {
|
||||
withOptional: true,
|
||||
withExtendedOptional: opts?.withExtendedDefaults ?? false,
|
||||
});
|
||||
}
|
||||
|
||||
const result = schema.validate(value, {
|
||||
|
||||
Reference in New Issue
Block a user