litmus icon indicating copy to clipboard operation
litmus copied to clipboard

Recursive Schemas?

Open chanon opened this issue 5 years ago • 3 comments

Hello, this is a very interesting library. I like that it has the ability to coerce data.

I am wondering though, as I don't see an example of doing so, does it support recursive schemas? Eg. giving a schema as the type for properties?

chanon avatar May 28 '19 16:05 chanon

I think that could make sense for certain types. The only one that comes immediately to mind is Litmus.Type.List. E.g. if you wanted to validate a list of strings that should be a certain length. Something like:

schema = %{
  "passcodes" => %Litmus.Type.List{
    max_length: 3,
    type: %Litmus.Type.String{
      min_length: 3,
      max_length: 32
    }
  }
}

Is that what you have in mind?

mgartner avatar Jun 06 '19 23:06 mgartner

Yes, something like that. Personally, I'm not using litmus yet, so no pressure from me, just a feature suggestion.

chanon avatar Jun 07 '19 17:06 chanon

I think it could also make sense where we have a nested map in params, for example:

{
  "id": "psc_12345",
  "resource": "postcard",
  "to": {
    "name": "Customer 1",
    "line1": "185 BERRY ST STE 6100",
    "city": "SAN FRANCISCO",
    "state": "CA",
    "zip": "94107",
    "country": "UNITED STATES"
  },
  "from": {
    "name": "Customer 2",
    "line1": "185 BERRY ST STE 6600",
    "city": "SAN FRANCISCO",
    "state": "CA",
    "zip": "94107",
    "country": "UNITED STATES"
  }
}

In this case both the to and from params could have nested Litmus schemas. Maps and lists might be the only two cases I can think of for which recursive/nested schemas could be useful. I'd like to try implementing this at some point, since it could be a good feature!

mpiercy827 avatar Jul 09 '19 21:07 mpiercy827