schema icon indicating copy to clipboard operation
schema copied to clipboard

Implement Predicate decorator

Open doloopwhile opened this issue 7 years ago • 2 comments

Hello. I added Predicate decorator.

Currently, To define a functional schema, We have to write a def statement and an assignment statement.

def some_condition(x):
   ...
   ...
   (Some validation logic longer than 10 lines)
   ...
   ...

some_condition.__name__ = "Error message" # When define it as naked function
some_condition = Schema(some_condition, error="Error message") # When define it as object

Predicate make it possible to define a functional schema as one def statement.

@Predicate(error="Error message")
def some_condition(x):
   ...
   ...

I feel later style is more readable (in the same reason of @getter defining properties).

doloopwhile avatar Feb 16 '18 09:02 doloopwhile

Thank you for your contribution! Hmm, this looks useful, but I don't know if the verbose way is currently so big a pain to justify including this predicate in the library. On the other hand, it can't hurt, but it does make the library a bit more complicated. You're literally replacing this:

some_condition = Schema(some_condition, error="Error message")

with this:

@Predicate(error="Error message")

correct? The latter is more readable, true, but I'm not sure that's not enough justification to include this. Does anyone else want to chime in?

skorokithakis avatar Feb 18 '18 12:02 skorokithakis

Codecov Report

Merging #146 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #146   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           1      1           
  Lines         208    212    +4     
=====================================
+ Hits          208    212    +4
Impacted Files Coverage Δ
schema.py 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 2eeccbe...730d978. Read the comment docs.

codecov-io avatar Feb 19 '18 00:02 codecov-io