magic.lambda.validators - Hyperlambda validators
This project contains argument validators for Hyperlambda. More specifically it contains the following slots.
- [validators.date] - Verifies that some date input is a date, and optionally between [min] and [max] value
- [validators.email] - Verifies that some input is a legal email address
- [validators.enum] - Verifies that some input is one of a set of predefined legal values, found as values of children
- [validators.integer] - Verifies that some integer input (long, int, etc) is between some specified [min] and [max] range
- [validators.mandatory] - Verifies that some input valus is given (at all)
- [validators.regex] - Verifies that some input is matching some given [regex] pattern
- [validators.string] - Verifies that some string input is between [min] and [max] in length
- [validators.url] - Verifies that some string input is a legal URL, either HTTP or HTTPS type of scheme
- [validators.recaptcha] - reCAPTCHA validator, to avoid bots from invoking your APIs
All of the above slots takes an expression, or values, as its main input, and will throw exceptions if their input expression’s value(s), or its value, does not follow the rules specified by the validator. Some of the above slots takes additional arguments. This makes them perfect fits for “intercepting” the input specified to an HTTP REST endpoint, to verify the input data conforms to some sort of predefined validation scheme.
Commonalities between validators
.foo email:firstname.lastname@example.org validators.email:x:@.foo/*/email
Most Hyperlambda validators requires some sort of argument(s) - However, some of these validators does not require arguments, such as the email validator, that simply verifies the input is a valid email address. To use the [validators.regex] validator, you should probably learn regular expression. However, this is beyond the scope of this article.
Notice - No attempt to invoke the type validator logic will be done unless the value is a non null value. If you want to enforce such logic, you’ll have to combine the specific type validators with the [validators.mandatory] validator, that enforces that a value must be specified and not have a null value.
How to use [validators.date]
This slot works similarly to the [validator.integer] validator, except instead of providing a min/max integer value, you’re expected to provide a min/max date value. Below is an example of how you could use it.
validators.date:x:@.arguments/*/date min:date:"2005-01-21T23:59:47" max:date:"2023-01-21T23:59:47"
How to use [validators.email]
This slot takes no arguments besides an expression, and will throw an exception unless the value of the node the expression is leading to is a valid email address. Below is example usage.
How to use [validators.enum]
This validator will throw an exception unless the specified string argument is one of the legal values. Usage could be as follows.
validators.enum:x:@.arguments/*/enum_value .:val1 .:val2
If the above [enum_value] is not either
val2 the validator will throw an exception.
How to use [validators.integer]
This value takes a [min] and [max] value, both of which are optional, and declares the minimum, and/or maximum value of the integer input. Usage could be as follows.
validators.integer:x:@.arguments/*/some-integer-argument min:50 max:100
If the specified integer value is not within the range of the min and max value, an exception will be thrown.
How to use [validators.mandatory]
This slot simply throws an exception if the specified argument is not supplied. Below is example usage.
How to use [validators.regex]
This validator requires a [regex] argument, that is a regular expression that must match the argument specified. Usage can be found below.
.arguments foo:howdy world validators.regex:x:@.arguments/*/foo regex:howdy
If you remove the “howdy” parts of your above argument, an exception will be thrown.
How to use [validators.string]
This works the same way as the [validators.integer] validator, except instead of being a min/max value the min/max arguments declares the minimum and maximum length of the string, allowing you to restrict string length of arguments to a min/max value for your Hyperlambda. Below is example usage that will throw an exception unless the argument is between 5 and 15 characters in length.
validators.string:x:@.some-arg min:5 max:15
How to use [validators.url]
This is similar to the [validators.email] slot, except it requires the argument to be a valid URL instead of a valid email. Below is example usage.
How to use [validators.recaptcha]
This is probably the most complex validator, and required 3 arguments.
- [site-key] - Site key as provided to you by Google’s reCAPTCHA admin panel
- [secret] - Secret as provided to you by Google’s reCAPTCHA admin panel
- [min] - Minimum value, a decimal value between 0 and 1. A good value here is typically 0.3
To use this validator you will need a Google reCAPTCHA account, version 3, at which point you can use it as follows.
validators.recaptcha:x:@.arguments/*/recaptcha_value site-key:xyz secret:qwerty min:decimal:0.3
You can use one invocation to any of the validators to validate multiple nodes, such as the following illustrates.
.arguments . no:5 . no:10 . // Throws if you remove the "." .no:11 validators.integer:x:@.arguments/*/*/no min:5 max:10
First the above expression will be evaluated, then every resulting value will be validated, and if any of them are not validated according to the validator’s arguments - Which for the above example is number between 5 and 10 - The validater will throw an exception, providing the invalid value, and the name of the last iterator (effectively being the argument name) to the caller. This allows you to use one single validator to validate multiple arguments, such as the above illustrates. This might be useful if you for instance have an endpoint accepting multiple address fields, and zip code is a mandatory argument, and it needs to be an integer with a [max] and [min] value.
Project website for magic.lambda.validators
The source code for this repository can be found at github.com/polterguy/magic.lambda.validators, and you can provide feedback, provide bug reports, etc at the same place.