Hyrule Constraints
In the last post we took a look at how to get started using the Hyrule validation framework. In this tutorial we will take a look at what constraints are and how they are the backbone of what this framework is trying to accomplish.
A constraint is a type of check or validation on a property. In the example from my last article we provided a constraint for the first and last name. The not empty is just one of many constraints available to you. This constraint just checks to make sure the data set for this property is not empty. You will obviously come across situations where you need to run other types of checks. Hyrule comes with a number of useful constraints out of the box. Please remember that anytime you see a bracket {} that you don't actually have to use the bracket, it just indicates a parameter.
| Constraint | Usage | Description |
|---|---|---|
| Assert True | @AssertTrue | check that the value is true |
| Assert False | @AssertFalse | check that the value is false |
| Array | @Array | check that the value is a valid array |
| Binary | @Binary | check that the value is a valid binary value |
| Boolean | @Boolean | check that the value is a valid boolean value |
| Credit Card Number | @CreditCardNumber | check that the value is a valid credit card number |
| Date | @Date | check that the value is a valid date |
| check that the value is a valid email address | ||
| Future | @Future | checks that the value is in the future relative to now() (default) or the date if a date is provided. |
| GUID | @GUID | check that the value is a valid GUID |
| In List | @InList {list} | check that the value is in the list provided |
| Is Match | @IsMatch {match} | |
| Lowercase | @Lowercase | check that the value is all lowercase |
| Max | @Min {max} | checks that a string is at least max number of characters or that a number is at least eqaul to max |
| Min | @Min {min} | checks that a string is at least min number of characters or that a number is at least eqaul to min |
| Not Null | @NotNull | checks that the value is not null. |
| Not Empty | @NotEmpty {trimWhitespace} | checks that a value is not empty. By default (@NotNull | @NotNull true) will trim the whitespace from a string. |
| Not In List | @NotInList {list} | checks that a value is not in the list provided |
| Numeric | @Numeric | checks that a value is numeric |
| Query | @Query | checks that a value is a query object |
| Password | @Password {min,max,level} | checks that the value is greater than min, less than max and passes security level. |
| Past | @Past {date} | checks that the value is in the past relative to now() (default) or the date if a date is provided. |
| Pattern | @Pattern {pattern} | |
| Phone | @Phone | checks that the value is a valid phone number. |
| Range | @Range {start,end} | checks that the value is greater than or equal to start and less than or equal end. |
| Regex | @Regex {pattern} | |
| Social Security Number | @SSN | checks that the value is a valid social security number. |
| String | @String | checks that the value is a valid string. |
| Struct | @Struct | checks that the value is a valid struct. |
| Size | @Size {min,max} | checks that the length of a list,array or struct falls between the min and max attribute values. |
| URL | @URL | checks that the value is a valid URL. |
| Uppercase | @Uppercase | checks that the value is a string in all uppercase. |
| UUID | @UUID | checks that the value is a valid UUID. |
| Zip Code | @ZipCode | checks that the value is a valid U.S., 5- or 9-digit format ZIP codes. |
Now that you have an idea of what constraints are available you can imagine the possibilities. Here we have a little bit more advanced user component.
This is a great start for validating data. This however is not going to be the magical answer for all of your validation needs. There are going to be times where you have to write your own constraints. With that in mind I have come up with a way for you to write your own constraints. We will take a look at this in the next article. Keep those comments & questions coming.
