Annotation-based conditional validation library.
Annotation-based conditional validation library.
A small .NET and JavaScript library which provides full-stack, annotation-based, conditional validation mechanisms.
Given attributes, powered by expressions engine, allow to forget about imperative way of step-by-step implementation of validation conditions in many cases. Since fields validation requirements are applied as metadata, domain-related code is more condensed.
There are number of cases where the concept of metadata is used for justified reasons. Attributes are one of the ways to associate complementary information with existing data. Such annotations may also define the correctness of data.
Declarative validation when compared to imperative approach seems to be more convenient in many cases. Clean, compact code - all validation logic defined within the model scope. Simple to write, obvious to read.
RequiredIf - if value is not yet provided, check whether it is required (annotated field is required to be non-null, when given condition is satisfied),AssertThat - if value is already provided, check whether the condition is met (non-null annotated field is considered as valid, when given condition is satisfied).ASP.NET MVC web sample is also hosted online - http://expressiveannotations.net/.
This section presents few exemplary code snippets. Sample projects in the section above contain much more comprehensive set of use cases.
using ExpressiveAnnotations.Attributes;
[RequiredIf("GoAbroad == true")]
public string PassportNumber { get; set; }Above we are saying, that annotated field is required when condition given in the logical expression is satisfied (passport number is required, if go abroad field has true boolean value).
Simple enough, let's move to another variation:
[AssertThat("ReturnDate >= Today()")]
public DateTime? ReturnDate { get; set; }By the usage of this attribute type, we are not validating field requirement as before - its value is allowed to be null this time. Nevertheless, if some value is already given, provided restriction needs to be satisfied (return date needs to be greater than or equal to the date returned by Today() built-in function).
As shown below, both types of attributes may be combined (moreover, the same type can be applied multiple times for a single field):
[RequiredIf("Details.Email != null")]
[RequiredIf("Details.Phone != null")]
[AssertThat("AgreeToContact == true")]
public bool? AgreeToContact { get; set; }Literal translation means, that if either email or phone is provided, you are forced to authorize someone to contact with you (boolean value indicating contact permission has to be true). What is more, we can see that nested properties are supported by the expressions parser.
The complexity of expressions may be arbitrarily increased, e.g. take a brief look at the following construction:
…Terminals are expressed in quotes. Each nonterminal is defined by a rule in the grammar except for dec-lit, bin-lit, hex-lit, float-lit, string-lit and identifier, which are assumed to be implicitly defined (identifier specifies names of functions, properties, constants and enums).
Expressions are built of Unicode letters and numbers (i.e. [L*] and [N*] categories respectively) with the usage of the following components:
!a, a||b, a&&b,a==b, a!=b, ab, a>=b,+a, -a, a+b, a-b, a*b, a/b, a%b, ~a, a&b, a^b, a|b, a<>b,a(), a[], a.b, a?b:c,null,true and false,123,0b prefix), e.g. 0b1010,0x prefix), e.g. 0xFF,1.5 or 0.3e-2,'in single quotes' (internal quote escape sequence is \', character representing new line is \n),[1,2,3],EA expressions syntax is defined by the grammar shown above.
Valid expressions must follow not only lexical and syntax level rules, but must have appropriate semantics as well.
Grammars, as already mentioned, naturally define the language syntax - but not only. Grammars in general contribute to the semantics a little, by defining the parse tree - thus denoting the precedence and associativity of operators. This being said, it is not just about the legal string being produced - the structure of parse tree corresponds to the order, in which various parts of the expression are to be evaluated.
Such contribution to semantics is not enough though. Grammar says nothing about the types of operands. For the expressions to have valid semantics, all the requirements on the types of operands must be realized as well.
Prior to execution of an operation, type checks and eventual type conversions are made. The result type, e.g. of a valid binary operation is, most of the time but not always, the same as the most general of the input types. When a binary operation is used then a generalization of operands is performed, to make the two operands the same (most general) type, before the operation is done. The order of generalization, from most general to most specific, is briefly described as follows: string (generalization done using the current locale) -> double -> int. Also nullable types are more general than their non-nullable counterparts.
The following table lists the precedence and associativity of operators (listed top to bottom, in descending precedence):
Precedence
Operator
Description
Associativity
1
()
Function call (postfix)
Left to right
[]
Subscript (postfix)
.
Member access (postfix)
2
+ -
Unary plus and minus
Right to left
! ~
Logical NOT and bitwise NOT (one's complement)
3
* / %
Multiplication, division, and remainder
Left to right
4
+ -
Addition and subtraction
5
<< >>
Bitwise left shift and right shift
6
< <=
Relational operators < and ≤ respectively
> >=
Relational operators > and ≥ respectively
7
== !=
Equality operators = and ≠ respectivelyNo open issues yet, or sync has not completed.