Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
E

ExpressiveAnnotations

> 编程语言
Open source

Annotation-based conditional validation library.

348 stars0 likes2 views
WebsiteGitHub

About

Annotation-based conditional validation library.

ExpressiveAnnotations[annotation-based conditional validation]

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.

Table of contents

  • What is the context behind this work?
  • RequiredIf vs. AssertThat - where is the difference?
  • Sample projects + demo
  • What are a brief examples of usage?
  • Declarative vs. imperative programming - what is it about?
  • EA expressions specification
    • Grammar definition
    • Where syntax meets semantics
    • Operators precedence and associativity
    • Built-in functions (methods ready to be used by expressions)
  • How to construct conditional validation attributes?
    • Signatures description
    • Implementation details outline
    • Traps (discrepancies between server- and client-side expressions evaluation)
  • What about the support of ASP.NET MVC client-side validation?
  • Frequently asked questions
    • Is it possible to compile all usages of annotations at once? (re server-side)
    • What if there is no built-in function I need? (re client and server-side)
    • Can I have custom utility-like functions outside of my models? (re server-side)
    • How to cope with values of custom types? (re client-side)
    • How to cope with dates given in non-standard formats? (re client-side)
    • What if "ea" variable is already used by another library? (re client-side)
    • How to control frequency of dependent fields validation? (re client-side)
    • Can I increase web console verbosity for debug purposes? (re client-side)
    • I prefer enum values to be rendered as numbers, is it allowed? (re client-side)
    • How to access one method from another? (re client-side)
    • How to access current control from inside a method? (re client-side)
    • How to fetch field's value or its display name in error message? (re client and server-side)
    • Is there any event raised when validation is done? (re client-side)
    • Can I decorate conditionally required fields with asterisks? (re client-side)
    • How to handle validation based on parent model field? (re client and server-side)
    • RequiredIf attribute is not working, what is wrong? (re client and server-side)
    • Few fields seem to be bypassed during validation, any clues? (re client-side)
    • Client-side validation doesn't work, how to troubleshoot it? (re client-side)
    • Is there a possibility to perform asynchronous validation? (re client-side, experimental)
    • What if my question is not covered by FAQ section?
  • Installation instructions
  • Contributors
  • License

What is the context behind this work?

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 vs. AssertThat - where is the difference?

  • 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).

Sample projects + demo

  • ASP.NET MVC web sample,
  • WPF MVVM desktop sample.

ASP.NET MVC web sample is also hosted online - http://expressiveannotations.net/.

What are a brief examples of usage?

This section presents few exemplary code snippets. Sample projects in the section above contain much more comprehensive set of use cases.

csharp
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:

csharp
[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):

csharp
[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:

  • logical operators: !a, a||b, a&&b,
  • comparison operators: a==b, a!=b, ab, a>=b,
  • arithmetic operators: +a, -a, a+b, a-b, a*b, a/b, a%b, ~a, a&b, a^b, a|b, a<>b,
  • other operators: a(), a[], a.b, a?b:c,
  • literals:
    • null, i.e. null,
    • boolean, i.e. true and false,
    • decimal integer, e.g. 123,
    • binary integer (with 0b prefix), e.g. 0b1010,
    • hexadecimal integer (with 0x prefix), e.g. 0xFF,
    • float, e.g. 1.5 or 0.3e-2,
    • string, e.g. 'in single quotes' (internal quote escape sequence is \', character representing new line is \n),
    • array (comma separated items within square brackets), e.g. [1,2,3],
    • identifier, i.e. names of functions, properties, constants and enums.
Where syntax meets semantics

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.

Operators precedence and associativity

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
        
            &lt;&lt; &gt;&gt;
        
        Bitwise left shift and right shift
    

    

        6
        
            &lt; &lt;=
        
        Relational operators &lt; and ≤ respectively
    

    

        
            &gt; &gt;=
        
        Relational operators &gt; and ≥ respectively
    

    

        7
        
            == !=
        
        Equality operators = and ≠ respectively

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言