Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

add support for maybe structs and maybe lists #236

Closed
gcanti opened this issue Oct 21, 2015 · 5 comments
Closed

add support for maybe structs and maybe lists #236

gcanti opened this issue Oct 21, 2015 · 5 comments

Comments

@gcanti
Copy link
Owner

gcanti commented Oct 21, 2015

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:

  • name is required
  • street and city must be strings or.. both null (EDIT)

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
Copy link

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
Copy link
Owner Author

gcanti commented Oct 21, 2015

opss...thanks @volkanunsal, fixed

@VinSpee
Copy link

VinSpee commented Oct 21, 2015

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

@gcanti
Copy link
Owner Author

gcanti commented Oct 25, 2015

About lists, perhaps it's better:

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

@gcanti
Copy link
Owner Author

gcanti commented Nov 2, 2015

Released in v0.6.10 and v0.7.6

@gcanti gcanti closed this as completed Nov 2, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants