wickman / pystachio

type-checked dictionary templating library for python
MIT License
91 stars 22 forks source link

Idea: Value constraints #10

Open kevints opened 11 years ago

kevints commented 11 years ago

Often we want to restrict the set of values on a field to a subset of those allowed by the type, and often the best place to do this is the data model definition itself. For example:

class Car(Struct):
  model = String
  max_speed = Integer().constrain(GreaterThan(10))
  serial = Required(String().constrain(Match(r'^[0-9]{7}-[0-9A-F]{3}$')))

goal: serializability: just as the pystachio schema can currently be shipped with the data, constraints should be shippable as well

non-goal: arbitrary code: while it's tempting to just add a callback field, doing so would impair portability

Usage example:

car = Car(max_speed='{{ speed }}').bind(speed=5)
car.interpolate() # should return a failure code.

For inspiration see postgresql CHECK constraints.

jsirois commented 8 years ago

@kevints are you in a position to code or review for this? I'm willing to be the complement either way.

kevints commented 8 years ago

@jsirois unfortunately I'm not in a position to do either right now.