gcanti / tcomb-form

Forms library for react
https://gcanti.github.io/tcomb-form
MIT License
1.16k stars 136 forks source link

add support for maybe structs and maybe lists #236

Closed gcanti closed 8 years ago

gcanti commented 8 years ago

Let's say you have this type:

const Type = t.struct({
  name: t.String,
  address: t.struct({
    street: t.String,
    city: t.String
  })
});

everything's ok, all fields are required. Now consider this type:

const Type = t.struct({
  name: t.String,
  address: t.maybe(t.struct({
    street: t.String,
    city: t.String
  }))
});

what does t.maybe(t.struct({... means? A natural answer would be:

So can we infer the following rules?

a maybe struct is null if and only if all its fields are Nil

a maybe list is null if and only if all its elements are Nil

I'd love to hear your opinion on this

/cc @VinSpee

volkanunsal commented 8 years ago

age and weight must be numbers or.. both null

I think you meant street and city? ;) In practice, I can see myself using this. So why not?

gcanti commented 8 years ago

opss...thanks @volkanunsal, fixed

VinSpee commented 8 years ago

I 100% agree. This is exactly what I would expect 

gcanti commented 8 years ago

About lists, perhaps it's better:

a maybe list is null if and only if its length === 0

gcanti commented 8 years ago

Released in v0.6.10 and v0.7.6