validator

Golang validator library, because none fitted my needs.
Uses a basic validation pattern for validation:
a basic method that takes the value to validate as input and returns true for successful validation, false otherwise.
As opposed to most major libraries,
this one considers empty/missing strings as "valid" on every pre-defined rule except "required".
As long as the value's empty, we ignore validation and give thumbs up, but if we actually have a value, we validate it.
Usage
Defining a schema
The first step is to build your model.
This library works in a field => []Rule association, thus expecting a model built on map[string][]Rule
(it provides the Schema type redefinition, which is just the map given just before).
The example below demonstrates a basic but realistic form schema.
schema := map[string][]Rule{
"FirstName": {rules.Required{}, rules.SizeBetween{Lower: 3, Upper: 20}},
"Email": {rules.Required{}, rules.Email{}},
"AdditionalInfos": {rules.SizeBetween{Lower: 20, Upper: 2000}},
}
In this schema, we can see the following things:
FirstName is required and must be between 3 and 20 characters long.
Email is required and must resemble an email
AdditionalInfos is optional, can then be left empty, but if provided, must be between 20 and 2000 characters long.
Alternatively, you can now use a schema builder, using the following syntax:
schema := validator.builder.
Field("FirstName").Required().Between(3, 20).
Field("Email").Required().Email().
Field("AdditionalInfos").Between(20, 2000).
Build()
Every pre-defined rule have a method (Between is an alias for SizeBetween, both do the same thing).
If you want to use custom rules, you can use the Custom(rule) method.
If you want to have a rule-less field (for joining the field's value to the result set), you simply can do
Field("your ruleless field") without any additional rule method. If you use another Field call afterwards, this field
will still be kept.
Using the validator
Before using it, you firstly must create it.
This is simply done by initializing the following data.
validator := Validator{
Schema: schema,
}
Using Validate(url.Values), you can directly pass your *http.Request.FormField property to the validator.
It returns two things: a map[string]string of successfully validated fields, and a map[string]string of erroneous
fields.
- Every field whose entire ruleset returned "OK" will be in the successfully validated fields map, the value being the
value obtained through the given
url.Values object.
- If a validation error happens, other rules won't be checked and the validation will stop, putting the error message
as value for the failed field inside the erroneous fields map.
The success map only contains fields that have been validated.
If you want to forcibly include a field without any kind of validation, you simply need to put an empty list of rules.
E.g. "UncheckedField": []Rule{}
Translations
You can pass custom translations for rules, as a map[string]string value (Validator{Messages: yourMessages}).
Keys must be the rule name, case-sensitive (e.g. Required for the rule of the same name).
By default, if it doesn't find any custom translation, for existing rules it'll use English translations, and for
your custom rules, it'll use an empty string, which mean that if you want to make custom rules, you must make your own
translation strings, associated with those rules.
Pre-defined rules
If you have a stable rule that you'd like to see in this library, PR are warmly welcome!
Additionally, if you'd like to see a given rule in this library, but don't have any code for it, open an issue
and I'll integrate it in time.
| Rule |
Description |
| Required |
The value must be defined and non-empty |
| Starts with |
The value must start with the given prefix |
| Ends with |
The value must end with the given suffix |
| Size between |
The value's length must be between given lower bound and higher bound, inclusive (lower <= value <= higher) |
| Boolean |
The value is either "true" or "false" |
| Integer |
The value is an integer numeric string |
| Float |
The value is a floating-point numeric string |
| Choice |
The value is in the given values list (empty list, or empty value in list returns false) |
| Regex |
The value must match the given compiled regexp |
| Email |
The value must "look like" an email. This doesn't perfectly validate that the entered value is an email, but gives enough faith in the fact that it looks like an email. |
| URL |
The value must be a valid URL |
| UUIDv4 |
The value must be a valid UUIDv4, case-insensitive |
Extending this library
This rule method pattern allow you to re-use existing rules inside your own, so if one doesn't fit your requirements,
you simply can re-develop it.
You'll have to make a struct that complies with the Rule interface, and add a message to the custom translation list,
so in case of validation error on one of your custom rules, the library will be able to return a coherent message.
See error message handling for more details and info.
Road-map
I still have some things to do with this library, PR are welcome.
Changelog
0.0.3
- Removed Values type, using url.Values by default
0.0.2
- Add some more documentation to the code
- Set up gitlab CI for unit tests (and possibly doc building?)
- Make a Schema Builder factory (fluent)
0.0.1
- Base validation mechanisms
- French and english translations
- Base predefined ruleset