Validation

高新成 edited this page Nov 20, 2018 · 16 revisions

Annotation based

  • Max(value,message) : Check that the character sequence (e.g. string) validated represents a number, and has a value less than or equal to the maximum value specified.
  • Min(value,message) : Check that the character sequence (e.g. string) being validated represents a number, and has a value more than or equal to the minimum value specified.
  • NotBlank(message) : Check that a character sequence is not 'null' nor empty after removing any leading or trailing whitespace.
  • Email : Checks that a given character sequence (e.g. string) is a well-formed email address.
  • AssertFalse(message) : Validates that the value passed is false.
  • AssertTrue(message) : Validates that the value passed is true.
  • Size(min,max,message) : Check that the length of an array is between min and max.
  • Length(min,max,message) : Check that the character sequence length is between min and max.
  • Range(min,max,message) : Check that the value of filed is between min and max.
  • Pattern(pattern,message) : Check that the character sequence (e.g. string) is match the specified regular expression.

Example

import hunt.validation;

class User : Valid
{
    mixin MakeValid;

    @Legnth(6, 32,"length must be between {{min}} and {{max}}")
    string username;

    @Range(18, 32)
    int age;

    @email()
    string email;
}

void main()
{
    auto user = new User;

    user.username = "Brian";
    user.age = 18;
    user.email = "zoujiaqing@gmail.com";

    auto result = user.valid();

    if (result.isValid() == false)
    {
        import std.stdio : writeln;
        foreach(key, message; result.messages())
        {
            writeln("%s: %s", key, message);
        }
    }
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.