Skip to content

Tags Reference

marrow16 edited this page Jan 21, 2023 · 4 revisions

When building validators from structs, Valix can read the special v8n tags on struct fields

type Example struct {
	Field string `v8n:"token[,token, ...]"`
}

where the tokens correspond to various property validation options - as listed here:

Token Purpose
&constraint-name{fields...} [Full reference] Adds a constraint to the property (shorthand way of specifying constraint without constraint: or constraints:[] prefix)
&[condition,...]constraint-name{fields...} [Full reference] Adds a conditional constraint to the property - the constraint is only checked when the condition(s) are met
&<expr>constraint-name{fields...} [Full reference] Adds a conditional constraint to the property - the constraint is only checked when the expr evaluates to true
constraint:constraint-name{fields...} [Full reference] Adds a constraint to the property (this token can be specified multiple times within the v8n tag. The constraint-name must be a Valix common constraint or a previously registered constraint. The constraint `fields` can optionally be set.
constraints:[constraint-name{},...] [Full reference] Adds multiple constraints to the property
mandatory [Full reference] Specifies the property must be present
mandatory:condition
      or
mandatory:[condition,...]
[Full reference] Specifies the property must be present under specified conditions
notNull [Full reference] Specifies the value for the property cannot be null
nullable [Full reference] Specifies the value for the property can be null (opposite of notNull)
only
      or
only:condition
      or
only:[condition,...]
[Full reference] Specifies that the property must not be present with other properties
optional [Full reference] Specifies the property does not have to be present (opposite of mandatory)
order:n [Full reference] Specifies the order in which the property should be validated
If any of the properties have a non-zero order set - all properties are checked in order (by order and then name)
required [Full reference] same as mandatory
required:condition
      or
required:[condition,...]
[Full reference] same as mandatory:condition
required_with:expr
      or
+:expr
[Full reference] Specifies the property is required according to the presence/non-presence of other properties (as determined by the expr)
You can also control the violation message used when the property is required but missing using a required_with_msg: or +msg: tag token
stop_on_first
      or
stop1st
[Full reference] Specifies that property validation should stop at the first constraint violation found
Note: This would be the equivalent of setting Stop on each constraint
type:type [Full reference] Specifies (overrides) the type expected for the JSON property value
Where type must be one of (case-insensitive):
   string, number, integer, boolean, object, array, datetime or any
unwanted:condition
      or
unwanted:[condition,...]
[Full reference] Adds unwanted condition(s) for the property - where condition is a condition token (that may have been set during validation)
If the unwanted condition(s) is met but the property is present then this is a validation violation
unwanted_with:expr
      or
-:expr
[Full reference] Specifies the property is unwanted according to the presence/non-presence of other properties (as determined by the expr)
You can also control the violation message used when the property is present but unwanted using an unwanted_with_msg: or -msg: tag token
when:condition
      or
when:[condition,...]
[Full reference] Adds when condition(s) for the property - where condition is a condition token (that may have been set during validation)
The property is only validated when these conditions are met
obj.constraint:constraint-name{fields...} [Full reference] Sets a constraint on an entire object or array
obj.ignoreUnknownProperties [Full reference] Sets an object (or array of objects) to ignore unknown properties (ignoring unknown properties means that the validator will not fail if an unknown property is found)
obj.ordered [Full reference] Sets the object validator to check properties in order
obj.unknownProperties:true|false [Full reference] Sets whether an object is to allow/ignore (true) or disallow (false) unknown properties
obj.when:condition
      or
obj.when:[condition,...]
[Full reference] Adds when condition(s) for the object or array - where condition is a condition token (that may have been set during validation)
The object/array is only validated when these conditions are met
arr.allowNulls
[Full reference] For array (slice) fields, specifies that array elements can be null
Clone this wiki locally