Language Independent
Validation Rules (LIVR)
2.0
Viktor Turskyi
CTO at WebbyLab
OSDN-UA 2017
Example: validate registration data
LIVR Spec 2.0
Rules description - http://livr-spec.org/
Test suite -
https://github.com/koorchik/LIVR/tree/master/te
st_suite
Existing implementations
Perl (supported by WebbyLab)
JavaScript (supported by WebbyLab)
PHP (supported by WebbyLab)
Erlang (supported by community)
Python (supported by community)
Ruby (supported by community)
Why we need it?
● We develop under different platforms
● We validate different types of data
● There is no universal validator in the world
10 issues of existing validators
Issue 1: Some validators pass
through fields which have no
validation rules described
Decision 1: Exclude all fields that do
not have validation rules described
Issue 2: Some validators use
procedural rules
Decision 2: Declarative validation.
Validation = Schema
Issue 3: Some validators describe
validation as a code
Decision 3: Validation = Data
Issue 4: Some validators fails on the
first error
Decision 4: Return together errors
for all fields
Issue 5: Some validators have non
standardized error messages.
Like “Field name is required”
Decision 5: Use error codes
Issue 6: Some validators have
numeric error codes
Decision 6: Understandable error
codes.
Like “NOT_POSITIVE_INTEGER”
Issue 7: Some validators do not
support hierarchical data structures
Decision 7: Support hierarchical
data structures of the box
Issue 8: Some validators are limited
by built-in rules
Decision 8: Support of custom rules
and aliases registration
Issue 9: Some validators have to
broad responsibility.
Like working with HTML etc
Decision 9: Single responsibility -
validation
This makes it multipurpose (user
input validation, configs validation,
contracts programming etc)
Issue 10: Some validators do not
support transformation of validation
data
Decision 10: Data pipelining in the
validator’s core
Example: validate registration data
Types of Rules
Just validation: ‘required’, ‘min_number’ etc
Meta rules: ‘nested_object’, ‘list_of’ etc
Modifiers: ‘trim’, ‘to_lc’, leave_only’ etc
Syntax details
# Short form
password: [ 'required', {min_length: 10} ]
# Full form
password: [ {required: [ ] }, { min_length: [10] } ]
Example: validate nested object
{
name: 'required',
phone: {max_length: 10},
address: { 'nested_object': {
city: 'required',
zip: ['required', 'positive_integer']
}}
}
Example: validate list
{
order_id: ['required', 'positive_integer'],
product_ids: { 'list_of': [
'required',
'positive_integer'
] }
}
Example: validate list of objects
{
order_id: ['required', 'positive_integer'],
products: [ 'not_empty_list', { 'list_of_objects': {
product_id: ['required','positive_integer'],
quantity: ['required', 'positive_integer']
}}]
}
Example: output modification
(pipelines)
{
email: [ 'trim', 'required', 'email', 'to_lc' ]
}
IMPLEMENTATION
Small universal core, all features are
in rules
For the core there is no difference between:
‘required’, ‘nested_object’, ‘trim’.
Custom rules
Aliased Rules (new feature)
Unified approach to types handling
How we deal with this:
● Perl (weak types with monomorphic
operators)
● JavaScript (weak types with polymorphic
operators)
● Python (strong types with polymorphic
operators)
● Erlang
● PHP
● Ruby
Output types matters
Input can be a string or a number but output
type should correspond to validation rule.
Input: {a: “23”, b: 23}
{a: ‘positive_integer’, b: ‘string’}
It means that you expect to get ‘a’ as positive
integer and ‘b’ as a string.
Output types matters
Input: {a: “23”, b: 23}
{a: ‘positive_integer’, b: ‘string’}
Output:
Perl a=”23” (looks like number), b=23 (can be used as
string)
JavaScript a=23 (type Number), b=”23” (type String)
Python=23 (type Int), b=”23” (type String)
Using LIVR for REST API
Request: POST /users
Response
which looks better than “Bad request” :)
Links
LIVR:
● LIVR Playground -
http://webbylab.github.io/livr-playground
● Specification https://github.com/koorchik/LIVR
● Universal test suite
https://github.com/koorchik/LIVR/tree/master/test_suite
● Article about LIVR - https://goo.gl/y1bTw1
Viktor Turskyi
viktor@webbylab.com
http://twitter.com/koorchik
https://github.com/koorchik
WebbyLab
http://webbylab.com

Language Independent Validation Rules 2.0, Viktor Turskyi, talk at OSDN 2017