zhammer / morning-cd

What was the first piece of music you listened to this morning?
MIT License
4 stars 0 forks source link

Setup validators #105

Closed zhammer closed 5 years ago

zhammer commented 5 years ago

As per discussion with evan, invalidator can look something like:

def submit_listen(listen_input: ListenInput):
    invalid_reason = listen_entity.check_invalid
    if invalid_reason:
        # handle invalid
    ...

(I guess this is where https://www.python.org/dev/peps/pep-0572/ would be nice)

zhammer commented 5 years ago

@evanhammer do you have a reference to where you read about this pattern? functional programming invalidation standards or something?

zhammer commented 5 years ago

This should be it for validators at the moment

evanhammer commented 5 years ago

Yes, I have examples, though they are not functional:

(in order of popularity)

  1. https://github.com/chriso/validator.js

    • If no error, isConstraint returns true. EBH: But this library only validates a single field against a single constraint.
  2. https://validatejs.org/

    • If there are no errors nothing is returned. Otherwise an object in this format is returned.
  3. https://github.com/eivindfjeldstad/validate

    • The .validate() function returns an array of validation errors. (EBH: Unclear what happens when no errors. I think it returns an empty array.)

I also looked into Folktale (a functional library of data structures) and its Validation object: https://folktale.origamitower.com/api/v2.3.0/en/folktale.validation.html