validations

A simple validation DSL.


License
Other
Install
pip install validations==0.1.1

Documentation

README

This document explains how to use the library.

Usage

First, you need to import the package into your code:

from validations.dsl import *

...

Then, follow these instructions for each function:

is_even(number)

Checks if a number is even.

Parameters:

  • number: int value.

Examples:

>> is_even(2)
>> True

>> is_even(1)
>> False       

is_odd(number)

Checks if a number is odd.

Parameters:

  • number: int value.

Examples:

>> is_odd(2)
>> False

>> is_odd(1)
>> True       

is_positive(number)

Checks if a number is positive or zero.

Parameters:

  • number: numeric value.

Examples:

>> is_positive(2)
>> True

>> is_positive(0.0)
>> True       

>> is_positive(-3.1)
>> False

non_positive(number)

Checks if a number is strictly negative

Parameters:

  • number: numeric value.

Examples:

>> non_positive(2)
>> False

>> non_positive(0.0)
>> False       

>> non_positive(-3.1)
>> True

is_alphabetic(string)

Checks if an string contains only letters.

Parameters:

  • string: string value.

Examples:

>> is_alphabetic("Hello world")
>> True

>> is_alphabetic("This w11l be f4ls3")
>> False       

is_alphanumeric(string)

Checks if a string contains only letters and numbers.

Parameters:

  • string: string value.

Examples:

>> is_alphanumeric("Hello world")
>> True

>> is_alphanumeric("This w0n'7 be f4ls3")
>> True       

>> is_alphanumeric("?????")
>> False

is_numeric(string)

Checks if a string contains only numbers.

Parameters:

  • string: string value.

Examples:

>> is_numeric("Hello world")
>> False

>> is_numeric("This w111 be f4ls3")
>> False       

>> is_numeric("1234")
>> True

>> is_numeric("3.1415")
>> False

is_valid_percentage(number)

Checks that a number that represents a percentage is within the proper bounds.

Parameters:

  • number: numeric value.

Examples:

>> is_valid_percentage(2)
>> False

>> is_valid_percentage(0.0)
>> True       

>> is_valid_percentage(1.)
>> True

>> is_valid_percentage(50.0)
>> False

def is_valid_timestamp(date, unit):

Checks that a number that represents a date as milliseconds or seconds is correct.

Parameters:

  • date: int.
  • unit: String. 'millis' if date is in milliseconds, 'seconds' if date is in seconds.

Examples:

>> is_valid_timestamp(2, unit='seconds')
>> False

>> is_valid_timestamp(1234567890, unit='millis')
>> False

>> is_valid_timestamp(1465496168, unit='seconds')
>> True

>> is_valid_timestamp(1465496168000, unit='millis')
>> True

present(field_name, dictionary)

Checks if a field is present in a dictionary.

Parameters:

  • field_name: string.
  • dictionary: dict instance.

Examples:

dictionary = {'a': 1, 'b': 2}
>> present('c', dictionary)
>> False

>> present('a', dictionary)
>> True

is_empty(iterable)

Checks if an iterable is empty

Parameters:

  • iterable: Iterable instance (a list, a set, a dictionary, etc).

Examples:

>> is_empty([])
>> True

>> is_empty([1,2,3])
>> False

>> is_empty({'a': 1})
>> False

>> is_empty({})
>> True

>> is_empty(set())
>> True

>> is_empty(set([1,2,3,4]))
>> False

non_empty(iterable)

Checks if an iterable has at least one element.

Parameters:

  • iterable: Iterable instance (a list, a set, a dictionary, etc).

Examples:

>> non_empty([])
>> False

>> non_empty([1,2,3])
>> True

>> non_empty({'a': 1})
>> True

>> non_empty({})
>> False

>> non_empty(set())
>> False

>> non_empty(set([1,2,3,4]))
>> True