solnic / drops

🛠️ Tools for working with data effectively - data contracts using types, schemas, domain validation rules, type-safe casting, and more.
Other
251 stars 4 forks source link

Enum support #51

Open torepettersen opened 3 months ago

torepettersen commented 3 months ago

Thanks again for this lovely library :blush:

Would you consider adding Enum's to this library? Seems like it is a common enough use case that it is suppored for JSON Schemas.

Not sure what the best way to add enum support would be, but I would be happy to contribute.

One option could be an enum type:

schema do
  %{
    required(:some_enum) => type(:enum, values: ["some_string", 1, :some_atom])
  }

The other option seems like adding a validation in Drops.Predicates.

  %{
    required(:some_enum) => type(:any, values: ["some_string", 1, :some_atom])
  }

Seems like we would need a validation either way. So might make sense to start there? Any maybe add a enum type where the values would be a required validation?

torepettersen commented 3 months ago

Sorry, didn't realice I could just use:

  %{
    required(:some_enum) => type(:any, in?: ["some_string", 1, :some_atom])
  }
solnic commented 3 months ago

Thanks for reporting this, I actually think there's a place for a first-class Enum type here