typebox icon indicating copy to clipboard operation
typebox copied to clipboard

Feature request: functions as defaults

Open StephenHaney opened this issue 1 year ago • 0 comments

I'd like to be able to use functions as defaults so that they can run when Value.Create is used.

// Desired syntax:
export const FileSchema = Type.Object({
  id: Type.String({ default: () => ulid() }), // arrow func
  createdDate: Type.Number({ default: Date.now }), // non-invoked function reference
});

// Later
Value.Create(FileSchema) // the defaults evaluate now

Desired behavior: defaults are evaluated at Value.Create time if they're a function Actual behavior: fields with a function as default are dropped from the created object with no error or warning

StephenHaney avatar Aug 30 '24 05:08 StephenHaney