Immutable Clean
#1073
-
The documentation rightfully notes that
What is the idiomatic way to clean values in a non-destructive way? Would it be |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@crishoj Hi, You can use either https://github.com/sinclairzx81/typebox/blob/master/src/value/parse/parse.ts#L43-L57 const ParseReducer: ReducerFunction[] = [
(_schema, _references, value) => Clone(value),
(schema, references, value) => Default(schema, references, value),
(schema, references, value) => Clean(schema, references, value),
(schema, references, value) => Convert(schema, references, value),
(schema, references, value) => { Assert(schema, references, value); return value },
(schema, references, value) => (HasTransform(schema, references) ? TransformDecode(schema, references, value) : value),
]
// ------------------------------------------------------------------
// ParseValue
// ------------------------------------------------------------------
function ParseValue<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R {
return ParseReducer.reduce((value, reducer) => reducer(schema, references, value), value) as R
} You can use this as a basis for custom value processing. Hope this helps! |
Beta Was this translation helpful? Give feedback.
@crishoj Hi,
You can use either
structuredClone(...)
orValue.Clone(...)
prior to callingValue.Clean(T, ...)
. There isn't really an established idiom for this (just Clone the value before calling mutable Value functions). However, if you're interested, TypeBox internally handles value pipeline operations in the following way (for it's Parse function)https://github.com/sinclairzx81/typebox/blob/master/src/value/parse/parse.ts#L43-L57