Validation

example_validation.py
 from pymonet.validation import Validation


 def test_validation_is_fail():
     assert Validation.fail(['fail']).is_fail()


 def validate_length(value):
     if len(value) < 5:
         return Validation.fail(['value not long enough'])
     return Validation.success()


 def validate_uppercase(value):
     if value[0].upper() != value[0]:
         return Validation.fail(['value not uppercase'])
     return Validation.success()


 def validate_contains_special_character(value):
     if re.match(r'^[a-zA-Z0-9_]*$', value):
         return Validation.fail(['value not contains special character'])
     return Validation.success()


 def validate(value):
     return (Validation.success(value)
             .ap(validate_length)
             .ap(validate_uppercase)
             .ap(validate_contains_special_character))


 validate('Success$') # Validation['Success$', []]
 validate('Success') # Validation['Success$', ['value not uppercase']]
 validate('S$') # Validation['Success$', ['value not long enough']]
 validate('s$') # Validation['Success$', ['value not long enough', 'value not uppercase']]
 validate('s') # Validation['Success$', ['value not long enough', 'value not uppercase', 'value not contains special character']]
class pymonet.validation.Validation(value, errors)

It that can hold either a success value or a failure value and has methods for accumulating errors

__eq__(other)

Two Validations are equals when values and errors lists are equal.

ap(fn)

It takes as a parameter function returning another Validation. Function is called with Validation value and returns new Validation with previous value and concated new and old errors.

Parameters

monad (Function(A) -> Validation[Any, List[E]]) – monad contains function

Returns

new validation with stored errors

Return type

Validation[A, List[E]]

bind(folder)

Take function and applied this function on current Validation value and returns folder result.

Parameters

mapper (Function(A) -> Validation[B, E]) – mapper function

Returns

new Validation with mapped value

Return type

Validation[B, E]

classmethod fail(errors=[])

Returns failed Validation with None as value and errors list.

Params errors

list of errors to store

Returns

Failed Validation

Return type

Validation[None, List[E]]

is_fail()

Returns True when errors list are not empty.

Returns

True for empty errors not list

Return type

Boolean

is_success()

Returns True when errors list are empty.

Returns

True for empty errors list

Return type

Boolean

map(mapper)

Take function (A) -> B and applied this function on current Validation value.

Parameters

mapper (Function(A) -> B) – mapper function

Returns

new Validation with mapped value and previous errors

Return type

Validation[B, List[E]]

classmethod success(value=None)

Returns successful Validation with value and empty errors list.

Params value

value to store in Validation

Returns

Successful Validation

Return type

Validation[A, []]

to_box()

Transform Validation to Box.

Returns

Box with Validation value

Return type

Box[A]

to_either()

Transform Validation to Either.

Returns

Right monad with previous value when Validation has no errors, in other case Left with errors list

Return type

Right[A] | Left[E]

to_lazy()

Transform Validation to Try.

Returns

Lazy monad with function returning Validation value

Return type

Lazy[Function() -> (A | None)]

to_maybe()

Transform Validation to Maybe.

Returns

Maybe with Validation Value when Validation has no errors, in other case empty Maybe

Return type

Maybe[A | None]

to_try()

Transform Validation to Try.

Returns

successfully Try with Validation value value. Try is successful when Validation has no errors

Return type

Try[A]