You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now the only difference between type and object struct is the "looseness" of the properties. type doesn't care about unknown properties, whereas object is "strict" and throws when unknown properties are received.
I was thinking it might make sense to just make type a subset of object that is accessed via a { loose: true } option:
Could also allow layering in the masked utility as { mask: true } as well, since it only applies to objects.
When doing either of these we'd ensure that object structs can be passed as schemes to new object structs, so that you can immutably change the settings, for example:
I use superstruct to validate configuration file. Sometimes someone might have a typo on the key field. I want the users to notice that mistake but I don't want to throw error and stop execution. So something like onExtraField will be useful.
constUser=object({id: number(),name: string(),nickname: optional(string())},{// `onExtraField` is only valid when loose is `true`, so `onExtraField` is under `loose`loose: {onExtraField: fieldName=>{console.log(`Unknown field '${fieldName}'`)}}})// Console output `Unknown field 'nicknmae'` without throwing errorUser.assert({id: 1,name: 'Tony Stark',// Typo in the field name herenicknmae: 'Iron Man'})
Curious to hear other perspectives on this.
Right now the only difference between
type
andobject
struct is the "looseness" of the properties.type
doesn't care about unknown properties, whereasobject
is "strict" and throws when unknown properties are received.I was thinking it might make sense to just make
type
a subset ofobject
that is accessed via a{ loose: true }
option:Might be easier to find/understand for new users.
The text was updated successfully, but these errors were encountered: