Closed aleclarson closed 5 months ago
@aleclarson Hi,
I like the idea of bailing out of checks and additional processing of values, but may need a bit more context on this. Note that Optional type is only applicable to PropertKeys of Objects where optionality is determined via key in value
checks (as opposed to value === undefined
) so this logic might make more sense added to the FromObject
method (where key enumeration happens). Also note that ability to bail out on Optional may be applicable to all types (so not limited to Union), which also suggests updates to FromObject.
Can you provide a bit more information (mostly code examples)
Just need to get my bearings on usage patterns before proceeding on this one.
@aleclarson Hey, just for reference purposes.
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'
const NumericDecode = (value: string | boolean) => {
console.log('decoding...')
return typeof value === 'boolean' ? value ? 1 : 0 : parseInt(value)
}
const Numeric = Type.Transform(Type.Union([Type.String(), Type.Boolean()]))
.Decode(NumericDecode)
.Encode((value) => value.toString())
const T = Type.Object({
x: Type.Optional(Numeric),
})
// Reference Example
console.log(Value.Decode(T, { })) // { } // note: that the 'decoding...' string is not logged as the
// x property key is not specified on the value.
console.log(Value.Decode(T, { x: true })) // { x: 1 }
console.log(Value.Decode(T, { x: false })) // { x: 0 }
console.log(Value.Decode(T, { x: '123' })) // { x: 123 }
The current logic does seem to be omitting decode call when the property is missing (avoiding the need to bail out). But if you have a use case / repro different from the above, just post that below.
Cheers!
@aleclarson Hey might close off this PR as the functionality seems to be working as expected here (and noting some the finer details around undefined
and optional
properties).
Generally, I think everything is fine here, but if you can draft up a repro and submit as an issue, can maybe take another things.
Thanks again!
Since the “optionality” of a Union type is not inherited by its anyOf types, we need to bail out on
undefined
values when the Union is optional.