github.com/System-Glitch/goyave/v2@v2.10.3-0.20200819142921-51011e75d504/docs/assets/js/23.931b2034.js (about)
1 (window.webpackJsonp=window.webpackJsonp||[]).push([[23],{395:function(t,a,e){"use strict";e.r(a);var s=e(25),n=Object(s.a)({},(function(){var t=this,a=t.$createElement,e=t._self._c||a;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"validation"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#validation"}},[t._v("#")]),t._v(" Validation")]),t._v(" "),e("p"),e("div",{staticClass:"table-of-contents"},[e("ul",[e("li",[e("a",{attrs:{href:"#introduction"}},[t._v("Introduction")])]),e("li",[e("a",{attrs:{href:"#rules-sets"}},[t._v("Rules sets")])]),e("li",[e("a",{attrs:{href:"#available-validation-rules"}},[t._v("Available validation rules")])]),e("li",[e("a",{attrs:{href:"#custom-rules"}},[t._v("Custom rules")]),e("ul",[e("li",[e("a",{attrs:{href:"#adding-a-message-to-your-rule"}},[t._v("Adding a message to your rule")])])])]),e("li",[e("a",{attrs:{href:"#validating-arrays"}},[t._v("Validating arrays")]),e("ul",[e("li",[e("a",{attrs:{href:"#n-dimensional-arrays"}},[t._v("N-dimensional arrays")])])])]),e("li",[e("a",{attrs:{href:"#placeholders"}},[t._v("Placeholders")]),e("ul",[e("li",[e("a",{attrs:{href:"#available-placeholders"}},[t._v("Available placeholders")])])])]),e("li",[e("a",{attrs:{href:"#manual-validation"}},[t._v("Manual validation")])]),e("li",[e("a",{attrs:{href:"#alternative-syntax"}},[t._v("Alternative syntax")])])])]),e("p"),t._v(" "),e("h2",{attrs:{id:"introduction"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#introduction"}},[t._v("#")]),t._v(" Introduction")]),t._v(" "),e("p",[t._v("Goyave provides a powerful, yet easy way to validate all incoming data, no matter its type or its format, thanks to a large number of validation rules.")]),t._v(" "),e("p",[t._v("Incoming requests are validated using "),e("strong",[t._v("rules set")]),t._v(", which associate rules with each expected field in the request.")]),t._v(" "),e("p",[t._v("Validation rules can "),e("strong",[t._v("alter the raw data")]),t._v(". That means that when you validate a field to be number, if the validation passes, you are ensured that the data you'll be using in your controller handler is a "),e("code",[t._v("float64")]),t._v(". Or if you're validating an IP, you get a "),e("code",[t._v("net.IP")]),t._v(" object.")]),t._v(" "),e("p",[t._v("If a request contains a field with a "),e("code",[t._v("nil")]),t._v("/"),e("code",[t._v("null")]),t._v(" value, and that this field doesn't have the "),e("code",[t._v("nullable")]),t._v(" rule, the field is "),e("strong",[t._v("removed")]),t._v(" entirely from the request.")]),t._v(" "),e("p",[t._v("Validation is automatic. You just have to define a rules set and assign it to a route. When the validation doesn't pass, the request is stopped and the validation errors messages are sent as a response, using the correct "),e("RouterLink",{attrs:{to:"/guide/advanced/localization.html"}},[t._v("language")]),t._v(". The HTTP response code of failed validation is "),e("strong",[t._v('422 "Unprocessable Entity"')]),t._v(", or "),e("strong",[t._v('400 "Bad Request"')]),t._v(" if the body could not be parsed.")],1),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("You can customize the validation error messages by editing "),e("code",[t._v("resources/lang/<language>/rules.json")]),t._v(". Learn more in the "),e("RouterLink",{attrs:{to:"/guide/advanced/localization.html"}},[t._v("localization")]),t._v(" section.")],1)]),t._v(" "),e("p",[t._v("The following features require the "),e("code",[t._v("validation")]),t._v(" package to be imported.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/System-Glitch/goyave/v2/validation"')]),t._v("\n")])])]),e("h2",{attrs:{id:"rules-sets"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#rules-sets"}},[t._v("#")]),t._v(" Rules sets")]),t._v(" "),e("p",[t._v("Rule sets are defined in the same package as the controller, typically in a separate file named "),e("code",[t._v("request.go")]),t._v(". Rule sets are named after the name of the controller handler they will be used with, and end with "),e("code",[t._v("Request")]),t._v(". For example, a rule set for the "),e("code",[t._v("Store")]),t._v(" handler will be named "),e("code",[t._v("StoreRequest")]),t._v(". If a rule set can be used for multiple handlers, consider using a name suited for all of them. The rules for a store operation are often the same for update operations, so instead of duplicating the set, create one unique set called "),e("code",[t._v("UpsertRequest")]),t._v(".")]),t._v(" "),e("p",[e("strong",[t._v("Example:")]),t._v(" ("),e("code",[t._v("http/controller/product/request.go")]),t._v(")")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\tStoreRequest validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RuleSet "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RuleSet"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"name"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"between:3,50"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"price"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"numeric"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"min:0.01"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"image"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"nullable"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"file"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"image"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"max:2048"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"count:1"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n \n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ...")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),e("div",{staticClass:"custom-block warning"},[e("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),e("p",[e("strong",[t._v("The order in which you assign rules is important")]),t._v(", as rules are executed in this order. The rules checking for the type of the data should "),e("strong",[t._v("always be first")]),t._v(", or after "),e("code",[t._v("required")]),t._v(" and "),e("code",[t._v("nullable")]),t._v(".")]),t._v(" "),e("p",[t._v("If a field is not "),e("strong",[t._v("required")]),t._v(" and is missing from the request, "),e("strong",[t._v("no rules are checked")]),t._v("!")])]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[e("code",[t._v("validation.RuleSet")]),t._v(" is an alias for "),e("code",[t._v("map[string][]string")]),t._v(".")])]),t._v(" "),e("hr"),t._v(" "),e("p",[t._v("Once your rules sets are defined, you need to assign them to your routes using the "),e("code",[t._v("Validate()")]),t._v(" method. Learn more about routing in the "),e("RouterLink",{attrs:{to:"/guide/basics/routing.html"}},[t._v("dedicated section")]),t._v(".")],1),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("router"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Post")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/product"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" product"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Store"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Validate")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("product"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("StoreRequest"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),e("h2",{attrs:{id:"available-validation-rules"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#available-validation-rules"}},[t._v("#")]),t._v(" Available validation rules")]),t._v(" "),e("div",{staticClass:"table"},[e("p",[e("a",{attrs:{href:"#required"}},[t._v("Required")]),t._v(" "),e("a",{attrs:{href:"#nullable"}},[t._v("Nullable")]),t._v(" "),e("a",{attrs:{href:"#numeric"}},[t._v("Numeric")]),t._v(" "),e("a",{attrs:{href:"#integer"}},[t._v("Integer")]),t._v(" "),e("a",{attrs:{href:"#min-value"}},[t._v("Min")]),t._v(" "),e("a",{attrs:{href:"#max-value"}},[t._v("Max")]),t._v(" "),e("a",{attrs:{href:"#between-min-max"}},[t._v("Between")]),t._v(" "),e("a",{attrs:{href:"#greater-than-field"}},[t._v("Greater than")]),t._v(" "),e("a",{attrs:{href:"#greater-than-equal-field"}},[t._v("Greater than or equal")]),t._v(" "),e("a",{attrs:{href:"#lower-than-field"}},[t._v("Lower than")]),t._v(" "),e("a",{attrs:{href:"#lower-than-equal-field"}},[t._v("Lower than or equal")]),t._v(" "),e("a",{attrs:{href:"#string"}},[t._v("String")]),t._v(" "),e("a",{attrs:{href:"#array-type"}},[t._v("Array")]),t._v(" "),e("a",{attrs:{href:"#distinct"}},[t._v("Distinct")]),t._v(" "),e("a",{attrs:{href:"#digits"}},[t._v("Digits")]),t._v(" "),e("a",{attrs:{href:"#regex-pattern"}},[t._v("Regex")]),t._v(" "),e("a",{attrs:{href:"#email"}},[t._v("Email")]),t._v(" "),e("a",{attrs:{href:"#size-value"}},[t._v("Size")]),t._v(" "),e("a",{attrs:{href:"#alpha"}},[t._v("Alpha")]),t._v(" "),e("a",{attrs:{href:"#alpha-dash"}},[t._v("Alpha dash")]),t._v(" "),e("a",{attrs:{href:"#alpha-num"}},[t._v("Alpha numeric")]),t._v(" "),e("a",{attrs:{href:"#starts-with-value1"}},[t._v("Starts with")]),t._v(" "),e("a",{attrs:{href:"#ends-with-value1"}},[t._v("Ends with")]),t._v(" "),e("a",{attrs:{href:"#in-value1-value2"}},[t._v("In")]),t._v(" "),e("a",{attrs:{href:"#not-in-value1-value2"}},[t._v("Not in")]),t._v(" "),e("a",{attrs:{href:"#in-array-field"}},[t._v("In array")]),t._v(" "),e("a",{attrs:{href:"#not-in-array-field"}},[t._v("Not in array")]),t._v(" "),e("a",{attrs:{href:"#timezone"}},[t._v("Timezone")]),t._v(" "),e("a",{attrs:{href:"#ip"}},[t._v("IP")]),t._v(" "),e("a",{attrs:{href:"#ipv4"}},[t._v("IPv4")]),t._v(" "),e("a",{attrs:{href:"#ipv6"}},[t._v("IPv6")]),t._v(" "),e("a",{attrs:{href:"#json"}},[t._v("JSON")]),t._v(" "),e("a",{attrs:{href:"#url"}},[t._v("URL")]),t._v(" "),e("a",{attrs:{href:"#uuid-version"}},[t._v("UUID")]),t._v(" "),e("a",{attrs:{href:"#bool"}},[t._v("Bool")]),t._v(" "),e("a",{attrs:{href:"#same-field"}},[t._v("Same")]),t._v(" "),e("a",{attrs:{href:"#different-field"}},[t._v("Different")]),t._v(" "),e("a",{attrs:{href:"#confirmed"}},[t._v("Confirmed")]),t._v(" "),e("a",{attrs:{href:"#file"}},[t._v("File")]),t._v(" "),e("a",{attrs:{href:"#mime-foo"}},[t._v("MIME")]),t._v(" "),e("a",{attrs:{href:"#image"}},[t._v("Image")]),t._v(" "),e("a",{attrs:{href:"#extension-foo"}},[t._v("Extension")]),t._v(" "),e("a",{attrs:{href:"#count-value"}},[t._v("Count")]),t._v(" "),e("a",{attrs:{href:"#count-min-value"}},[t._v("Count min")]),t._v(" "),e("a",{attrs:{href:"#count-max-value"}},[t._v("Count max")]),t._v(" "),e("a",{attrs:{href:"#count-between-min-max"}},[t._v("Count between")]),t._v(" "),e("a",{attrs:{href:"#date-format"}},[t._v("Date")]),t._v(" "),e("a",{attrs:{href:"#before-date"}},[t._v("Before")]),t._v(" "),e("a",{attrs:{href:"#before-equal-date"}},[t._v("Before or equal")]),t._v(" "),e("a",{attrs:{href:"#after-date"}},[t._v("After")]),t._v(" "),e("a",{attrs:{href:"#after-equal-date"}},[t._v("After or equal")]),t._v(" "),e("a",{attrs:{href:"#date-equals-date"}},[t._v("Date equals")]),t._v(" "),e("a",{attrs:{href:"#date-between-date1-date2"}},[t._v("Date between")])])]),e("h4",{attrs:{id:"required"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#required"}},[t._v("#")]),t._v(" required")]),t._v(" "),e("p",[t._v("The field under validation must be present.")]),t._v(" "),e("p",[t._v("If the field is a string, the string must not be empty. If a field is "),e("code",[t._v("null")]),t._v(" and has the "),e("code",[t._v("nullable")]),t._v(" rule, the "),e("code",[t._v("required")]),t._v(" rules passes. As non-nullable fields are removed if they have a "),e("code",[t._v("null")]),t._v(" value, the "),e("code",[t._v("required")]),t._v(" rule doesn't pass if a field is "),e("code",[t._v("null")]),t._v(" and doesn't have the "),e("code",[t._v("nullable")]),t._v(" rule.")]),t._v(" "),e("h4",{attrs:{id:"nullable"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#nullable"}},[t._v("#")]),t._v(" nullable")]),t._v(" "),e("p",[t._v("The field under validation can have a "),e("code",[t._v("nil")]),t._v("/"),e("code",[t._v("null")]),t._v(" value. If this rule is missing from the rules set, the field will be "),e("strong",[t._v("removed")]),t._v(" if it is "),e("code",[t._v("null")]),t._v(". This rule is especially useful when working with JSON requests or with primitives that can contain null values.")]),t._v(" "),e("p",[t._v("Be sure to check if your field is not null before using it in your handlers.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// In this example, field is numeric")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" val"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" exists "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" request"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Data"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"field"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" exists "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&&")]),t._v(" val "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n field"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" request"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Data"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"field"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h4",{attrs:{id:"numeric"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#numeric"}},[t._v("#")]),t._v(" numeric")]),t._v(" "),e("p",[t._v("The field under validation must be numeric. Strings that can be converted to numbers are accepted.\nThis rule converts the field to "),e("code",[t._v("float64")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"integer"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#integer"}},[t._v("#")]),t._v(" integer")]),t._v(" "),e("p",[t._v("The field under validation must be an integer. Strings that can be converted to an integer are accepted.\nThis rule converts the field to "),e("code",[t._v("int")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"min-value"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#min-value"}},[t._v("#")]),t._v(" min:value")]),t._v(" "),e("p",[t._v("Depending on its type, the field under validation must be at least "),e("code",[t._v("value")]),t._v(".\nStrings, numerics, array, and files are evaluated using the same method as the "),e("a",{attrs:{href:"#size-value"}},[e("code",[t._v("size")])]),t._v(" rule.")]),t._v(" "),e("h4",{attrs:{id:"max-value"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#max-value"}},[t._v("#")]),t._v(" max:value")]),t._v(" "),e("p",[t._v("Depending on its type, the field under validation must not be superior to "),e("code",[t._v("value")]),t._v(".\nStrings, numerics, array, and files are evaluated using the same method as the "),e("a",{attrs:{href:"#size-value"}},[e("code",[t._v("size")])]),t._v(" rule.")]),t._v(" "),e("h4",{attrs:{id:"between-min-max"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#between-min-max"}},[t._v("#")]),t._v(" between:min,max")]),t._v(" "),e("p",[t._v("Depending on its type, the field under validation must be between "),e("code",[t._v("min")]),t._v(" and "),e("code",[t._v("max")]),t._v(".\nStrings, numerics, array, and files are evaluated using the same method as the "),e("a",{attrs:{href:"#size-value"}},[e("code",[t._v("size")])]),t._v(" rule.")]),t._v(" "),e("h4",{attrs:{id:"greater-than-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#greater-than-field"}},[t._v("#")]),t._v(" greater_than:field")]),t._v(" "),e("p",[t._v("The field under validation must be greater than the given "),e("code",[t._v("field")]),t._v(". The two fields must have the same type.\nStrings, numerics, array, and files are evaluated using the same method as the "),e("a",{attrs:{href:"#size-value"}},[e("code",[t._v("size")])]),t._v(" rule.")]),t._v(" "),e("h4",{attrs:{id:"greater-than-equal-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#greater-than-equal-field"}},[t._v("#")]),t._v(" greater_than_equal:field")]),t._v(" "),e("p",[t._v("The field under validation must be greater or equal to the given "),e("code",[t._v("field")]),t._v(". The two fields must have the same type.\nStrings, numerics, array, and files are evaluated using the same method as the "),e("a",{attrs:{href:"#size-value"}},[e("code",[t._v("size")])]),t._v(" rule.")]),t._v(" "),e("h4",{attrs:{id:"lower-than-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#lower-than-field"}},[t._v("#")]),t._v(" lower_than:field")]),t._v(" "),e("p",[t._v("The field under validation must be lower than the given "),e("code",[t._v("field")]),t._v(". The two fields must have the same type.\nStrings, numerics, array, and files are evaluated using the same method as the "),e("a",{attrs:{href:"#size-value"}},[e("code",[t._v("size")])]),t._v(" rule.")]),t._v(" "),e("h4",{attrs:{id:"lower-than-equal-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#lower-than-equal-field"}},[t._v("#")]),t._v(" lower_than_equal:field")]),t._v(" "),e("p",[t._v("The field under validation must be lower or equal to the given "),e("code",[t._v("field")]),t._v(". The two fields must have the same type.\nStrings, numerics, array, and files are evaluated using the same method as the "),e("a",{attrs:{href:"#size-value"}},[e("code",[t._v("size")])]),t._v(" rule.")]),t._v(" "),e("h4",{attrs:{id:"string"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#string"}},[t._v("#")]),t._v(" string")]),t._v(" "),e("p",[t._v("The field under validation must be a string.")]),t._v(" "),e("h4",{attrs:{id:"array-type"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#array-type"}},[t._v("#")]),t._v(" array:type")]),t._v(" "),e("p",[t._v("The field under validation must be an array. The "),e("code",[t._v("type")]),t._v(" parameter is "),e("strong",[t._v("optional")]),t._v(".")]),t._v(" "),e("p",[t._v("If no type is provided, the field has the type "),e("code",[t._v("[]interface{}")]),t._v(" after validation. If a type is provided, the array is converted to a slice of the correct type, and all values in the array are validated in the same way as standard fields.")]),t._v(" "),e("p",[t._v("For example, with the rule "),e("code",[t._v("array:url")]),t._v(", all values must be valid URLs and the field will be converted to "),e("code",[t._v("[]*url.URL")]),t._v(".")]),t._v(" "),e("p",[e("strong",[t._v("Available types:")])]),t._v(" "),e("ul",[e("li",[e("code",[t._v("string")])]),t._v(" "),e("li",[e("code",[t._v("numeric")])]),t._v(" "),e("li",[e("code",[t._v("integer")])]),t._v(" "),e("li",[e("code",[t._v("timezone")])]),t._v(" "),e("li",[e("code",[t._v("ip")]),t._v(", "),e("code",[t._v("ipv4")]),t._v(", "),e("code",[t._v("ipv6")])]),t._v(" "),e("li",[e("code",[t._v("url")])]),t._v(" "),e("li",[e("code",[t._v("uuid")])]),t._v(" "),e("li",[e("code",[t._v("bool")])]),t._v(" "),e("li",[e("code",[t._v("date")])])]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("For the "),e("code",[t._v("uuid")]),t._v(" and "),e("code",[t._v("date")]),t._v(" types, you can pass a second parameter: "),e("code",[t._v("array:date,02-01-2006")])])]),t._v(" "),e("h4",{attrs:{id:"distinct"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#distinct"}},[t._v("#")]),t._v(" distinct")]),t._v(" "),e("p",[t._v("The field under validation must be an array and have distinct values.")]),t._v(" "),e("h4",{attrs:{id:"digits"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#digits"}},[t._v("#")]),t._v(" digits")]),t._v(" "),e("p",[t._v("The field under validation must be a string and contain only digits.")]),t._v(" "),e("h4",{attrs:{id:"regex-pattern"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#regex-pattern"}},[t._v("#")]),t._v(" regex:pattern")]),t._v(" "),e("p",[t._v("The field under validation must be a string and match the given "),e("code",[t._v("pattern")]),t._v(".")]),t._v(" "),e("h4",{attrs:{id:"email"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#email"}},[t._v("#")]),t._v(" email")]),t._v(" "),e("p",[t._v("The field under validation must be a string and be an email address.")]),t._v(" "),e("div",{staticClass:"custom-block warning"},[e("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),e("p",[t._v("This rule is not enough to properly validate email addresses. The only way to ensure an email address is valid is by sending a confirmation email.")])]),t._v(" "),e("h4",{attrs:{id:"size-value"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#size-value"}},[t._v("#")]),t._v(" size:value")]),t._v(" "),e("p",[t._v("Depending on its type, the field under validation must:")]),t._v(" "),e("ul",[e("li",[t._v("Strings: have a length of "),e("code",[t._v("value")]),t._v(" characters.")]),t._v(" "),e("li",[t._v("Numerics: be equal to "),e("code",[t._v("value")]),t._v(".")]),t._v(" "),e("li",[t._v("Arrays: exactly have "),e("code",[t._v("value")]),t._v(" items.")]),t._v(" "),e("li",[t._v("Files: weight exactly "),e("code",[t._v("value")]),t._v(" KiB.\n"),e("ul",[e("li",[e("em",[t._v("Note: for this rule only (not for "),e("code",[t._v("min")]),t._v(", "),e("code",[t._v("max")]),t._v(", etc), the size of the file under validation is "),e("strong",[t._v("rounded")]),t._v(" to the closest KiB.")])]),t._v(" "),e("li",[t._v("When the field is a multi-files upload, the size of "),e("strong",[t._v("all files")]),t._v(" is checked.")])])])]),t._v(" "),e("div",{staticClass:"custom-block warning"},[e("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),e("p",[t._v("If the value cannot be validated because its type is unsupported, the rule passes. Therefore, you should always validate the type of the field "),e("strong",[t._v("before")]),t._v(" using the "),e("code",[t._v("size")]),t._v(", "),e("code",[t._v("min")]),t._v(", "),e("code",[t._v("lower_than")]),t._v(", ... rules.")])]),t._v(" "),e("h4",{attrs:{id:"alpha"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#alpha"}},[t._v("#")]),t._v(" alpha")]),t._v(" "),e("p",[t._v("The field under validation must be a string and be entirely alphabetic characters.")]),t._v(" "),e("h4",{attrs:{id:"alpha-dash"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#alpha-dash"}},[t._v("#")]),t._v(" alpha_dash")]),t._v(" "),e("p",[t._v("The field under validation must be a string and be entirely alphabetic-numeric characters, dashes or underscores.")]),t._v(" "),e("h4",{attrs:{id:"alpha-num"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#alpha-num"}},[t._v("#")]),t._v(" alpha_num")]),t._v(" "),e("p",[t._v("The field under validation must be a string and be entirely alphabetic-numeric characters.")]),t._v(" "),e("h4",{attrs:{id:"starts-with-value1"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#starts-with-value1"}},[t._v("#")]),t._v(" starts_with:value1,...")]),t._v(" "),e("p",[t._v("The field under validation must be a string and start with of the given values.")]),t._v(" "),e("h4",{attrs:{id:"ends-with-value1"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#ends-with-value1"}},[t._v("#")]),t._v(" ends_with:value1,...")]),t._v(" "),e("p",[t._v("The field under validation must be a string and end with one of the given values.")]),t._v(" "),e("h4",{attrs:{id:"in-value1-value2"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#in-value1-value2"}},[t._v("#")]),t._v(" in:value1,value2,...")]),t._v(" "),e("p",[t._v("The field under validation must be a one of the given values. Only numerics and strings are checked.")]),t._v(" "),e("h4",{attrs:{id:"not-in-value1-value2"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#not-in-value1-value2"}},[t._v("#")]),t._v(" not_in:value1,value2,...")]),t._v(" "),e("p",[t._v("The field under validation must not be a one of the given values. Only numerics and strings are checked.")]),t._v(" "),e("h4",{attrs:{id:"in-array-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#in-array-field"}},[t._v("#")]),t._v(" in_array:field")]),t._v(" "),e("p",[t._v("The field under validation must be a one of the values in the given "),e("code",[t._v("field")]),t._v(". Only numerics and strings are checked, and the given "),e("code",[t._v("field")]),t._v(" must be an array.")]),t._v(" "),e("h4",{attrs:{id:"not-in-array-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#not-in-array-field"}},[t._v("#")]),t._v(" not_in_array:field")]),t._v(" "),e("p",[t._v("The field under validation must not be a one of the values in the given "),e("code",[t._v("field")]),t._v(". Only numerics and strings are checked, and the given "),e("code",[t._v("field")]),t._v(" must be an array.")]),t._v(" "),e("h4",{attrs:{id:"timezone"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#timezone"}},[t._v("#")]),t._v(" timezone")]),t._v(" "),e("p",[t._v("The field under validation must be a string and be a valid timezone. This rule converts the field to "),e("code",[t._v("*time.Location")]),t._v(" if it passes.")]),t._v(" "),e("p",[t._v("Valid timezones are:")]),t._v(" "),e("ul",[e("li",[t._v("UTC")]),t._v(" "),e("li",[t._v("Timezones from the IANA Time Zone database, such as "),e("code",[t._v("America/New_York")])])]),t._v(" "),e("p",[t._v("The time zone database needed by LoadLocation may not be present on all systems, especially non-Unix systems. The rules looks in the directory or uncompressed zip file\nnamed by the "),e("code",[t._v("ZONEINFO")]),t._v(" environment variable, if any, then looks in known installation locations on Unix systems, and finally looks in "),e("code",[t._v("$GOROOT/lib/time/zoneinfo.zip")]),t._v(".")]),t._v(" "),e("h4",{attrs:{id:"ip"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#ip"}},[t._v("#")]),t._v(" ip")]),t._v(" "),e("p",[t._v("The field under validation must be a string and be either an IPv4 address or an IPv6 address.\nThis rule converts the field to "),e("code",[t._v("net.IP")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"ipv4"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#ipv4"}},[t._v("#")]),t._v(" ipv4")]),t._v(" "),e("p",[t._v("The field under validation must be a string and be an IPv4 address.\nThis rule converts the field to "),e("code",[t._v("net.IP")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"ipv6"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#ipv6"}},[t._v("#")]),t._v(" ipv6")]),t._v(" "),e("p",[t._v("The field under validation must be a string and be an IPv6 address.\nThis rule converts the field to "),e("code",[t._v("net.IP")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"json"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#json"}},[t._v("#")]),t._v(" json")]),t._v(" "),e("p",[t._v("The field under validation must be a valid JSON string. This rule unmarshals the string and sets the field value to the unmarshalled result.")]),t._v(" "),e("h4",{attrs:{id:"url"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#url"}},[t._v("#")]),t._v(" url")]),t._v(" "),e("p",[t._v("The field under validation must be a valid URL.\nThis rule converts the field to "),e("code",[t._v("*url.URL")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"uuid-version"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#uuid-version"}},[t._v("#")]),t._v(" uuid:version")]),t._v(" "),e("p",[t._v("The field under validation must be a string and a valid UUID.")]),t._v(" "),e("p",[t._v("The "),e("code",[t._v("version")]),t._v(" parameter is "),e("strong",[t._v("optional")]),t._v(".")]),t._v(" "),e("ul",[e("li",[t._v("If a "),e("code",[t._v("version")]),t._v(" is given ("),e("code",[t._v("uuid:3")]),t._v(","),e("code",[t._v("uuid:4")]),t._v(","),e("code",[t._v("uuid:5")]),t._v("), the rule will pass only if the version of the UUID matches.")]),t._v(" "),e("li",[t._v("If no "),e("code",[t._v("version")]),t._v(" is given, any UUID version is accepted.")])]),t._v(" "),e("p",[t._v("This rule converts the field to "),e("code",[t._v("uuid.UUID")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"bool"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#bool"}},[t._v("#")]),t._v(" bool")]),t._v(" "),e("p",[t._v("The field under validation must be a boolean or one of the following values:")]),t._v(" "),e("ul",[e("li",[e("code",[t._v("1")]),t._v("/"),e("code",[t._v("0")])]),t._v(" "),e("li",[e("code",[t._v('"1"')]),t._v("/"),e("code",[t._v('"0"')])]),t._v(" "),e("li",[e("code",[t._v('"on"')]),t._v("/"),e("code",[t._v('"off"')])]),t._v(" "),e("li",[e("code",[t._v('"true"')]),t._v("/"),e("code",[t._v('"false"')])]),t._v(" "),e("li",[e("code",[t._v('"yes"')]),t._v("/"),e("code",[t._v('"no"')])])]),t._v(" "),e("p",[t._v("This rule converts the field to "),e("code",[t._v("bool")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"same-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#same-field"}},[t._v("#")]),t._v(" same:field")]),t._v(" "),e("p",[t._v("The field under validation must have the same value as the given "),e("code",[t._v("field")]),t._v(". For arrays, the two fields must have the same values in the same order.")]),t._v(" "),e("p",[t._v("The two fields must have the same type. Files are not checked.")]),t._v(" "),e("h4",{attrs:{id:"different-field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#different-field"}},[t._v("#")]),t._v(" different:field")]),t._v(" "),e("p",[t._v("The field under validation must have a different value from the given "),e("code",[t._v("field")]),t._v(". For arrays, the two fields must have different values or not be in the same order.")]),t._v(" "),e("p",[t._v("The two fields must have the same type. Files are not checked.")]),t._v(" "),e("h4",{attrs:{id:"confirmed"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#confirmed"}},[t._v("#")]),t._v(" confirmed")]),t._v(" "),e("p",[t._v("The field under validation must have a matching "),e("code",[t._v("foo_confirmation")]),t._v(". This rule validate equality in the same way as the "),e("a",{attrs:{href:"#same-field"}},[e("code",[t._v("same")])]),t._v(" rule.")]),t._v(" "),e("p",[t._v("For example, if the field under validation is "),e("code",[t._v("password")]),t._v(", a matching "),e("code",[t._v("password_confirmation")]),t._v(" field must be present in the input.")]),t._v(" "),e("h4",{attrs:{id:"file"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#file"}},[t._v("#")]),t._v(" file")]),t._v(" "),e("p",[t._v("The field under validation must be a file. Multi-files are supported.\nThis rule converts the field to "),e("code",[t._v("[]filesystem.File")]),t._v(" if it passes.")]),t._v(" "),e("h4",{attrs:{id:"mime-foo"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#mime-foo"}},[t._v("#")]),t._v(" mime:foo,...")]),t._v(" "),e("p",[t._v("The field under validation must be a file and match one of the given MIME types. If the field is a multi-files, all files must satisfy this rule.")]),t._v(" "),e("h4",{attrs:{id:"image"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#image"}},[t._v("#")]),t._v(" image")]),t._v(" "),e("p",[t._v("The field under validation must be a file and match one of the following MIME types:")]),t._v(" "),e("ul",[e("li",[e("code",[t._v("image/jpeg")])]),t._v(" "),e("li",[e("code",[t._v("image/png")])]),t._v(" "),e("li",[e("code",[t._v("image/gif")])]),t._v(" "),e("li",[e("code",[t._v("image/bmp")])]),t._v(" "),e("li",[e("code",[t._v("image/svg+xml")])]),t._v(" "),e("li",[e("code",[t._v("image/webp")])])]),t._v(" "),e("p",[t._v("If the field is a multi-files, all files must satisfy this rule.")]),t._v(" "),e("h4",{attrs:{id:"extension-foo"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#extension-foo"}},[t._v("#")]),t._v(" extension:foo,...")]),t._v(" "),e("p",[t._v("The field under validation must be a file and match one of the given extensions. If the field is a multi-files, all files must satisfy this rule.")]),t._v(" "),e("h4",{attrs:{id:"count-value"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#count-value"}},[t._v("#")]),t._v(" count:value")]),t._v(" "),e("p",[t._v("The field under validation must be a multi-file and contain "),e("code",[t._v("value")]),t._v(" files.")]),t._v(" "),e("h4",{attrs:{id:"count-min-value"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#count-min-value"}},[t._v("#")]),t._v(" count_min:value")]),t._v(" "),e("p",[t._v("The field under validation must be a multi-file and contain at least "),e("code",[t._v("value")]),t._v(" files.")]),t._v(" "),e("h4",{attrs:{id:"count-max-value"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#count-max-value"}},[t._v("#")]),t._v(" count_max:value")]),t._v(" "),e("p",[t._v("The field under validation must be a multi-file and may not contain more than "),e("code",[t._v("value")]),t._v(" files.")]),t._v(" "),e("h4",{attrs:{id:"count-between-min-max"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#count-between-min-max"}},[t._v("#")]),t._v(" count_between:min,max")]),t._v(" "),e("p",[t._v("The field under validation must be a multi-file and contain between "),e("code",[t._v("min")]),t._v(" and "),e("code",[t._v("max")]),t._v(" files.")]),t._v(" "),e("h4",{attrs:{id:"date-format"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#date-format"}},[t._v("#")]),t._v(" date:format")]),t._v(" "),e("p",[t._v("The field under validation must be a string representing a date. The "),e("code",[t._v("format")]),t._v(" is optional. If no format is given, the "),e("code",[t._v("2006-01-02")]),t._v(" format is used.")]),t._v(" "),e("p",[t._v("This rule converts the field to "),e("code",[t._v("time.Time")]),t._v(" if it passes.")]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("See the "),e("a",{attrs:{href:"https://golang.org/src/time/format.go",target:"_blank",rel:"noopener noreferrer"}},[t._v("Golang datetime format"),e("OutboundLink")],1),t._v(".")])]),t._v(" "),e("div",{staticClass:"custom-block warning"},[e("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),e("p",[t._v("When validating dates by comparing them together, the order of the declaration of the fields in the request is important. For example, if you want to validate that an end date is after a start date, the start date should be declared "),e("strong",[t._v("before")]),t._v(" the end date in the rules set.")]),t._v(" "),e("p",[t._v("If a date has not been validated and converted yet, the date comparison rules will attempt to parse the dates using the following format: "),e("code",[t._v("2006-01-02")]),t._v(".")])]),t._v(" "),e("h4",{attrs:{id:"before-date"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#before-date"}},[t._v("#")]),t._v(" before:date")]),t._v(" "),e("p",[t._v("The field under validation must be a value preceding the given date. The "),e("code",[t._v("date")]),t._v(" must be written using following format: "),e("code",[t._v("2006-01-02T15:04:05")]),t._v(".")]),t._v(" "),e("p",[t._v("If the name of another field is given as a "),e("code",[t._v("date")]),t._v(", then the two fields must be a date and the field under validation must be preceding the given field.")]),t._v(" "),e("h4",{attrs:{id:"before-equal-date"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#before-equal-date"}},[t._v("#")]),t._v(" before_equal:date")]),t._v(" "),e("p",[t._v("The field under validation must be a value preceding or equal to the given date. The "),e("code",[t._v("date")]),t._v(" must be written using following format: "),e("code",[t._v("2006-01-02T15:04:05")]),t._v(".")]),t._v(" "),e("p",[t._v("If the name of another field is given as a "),e("code",[t._v("date")]),t._v(", then the two fields must be a date and the field under validation must be preceding or equal to given field.")]),t._v(" "),e("h4",{attrs:{id:"after-date"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#after-date"}},[t._v("#")]),t._v(" after:date")]),t._v(" "),e("p",[t._v("The field under validation must be a value after the given date. The "),e("code",[t._v("date")]),t._v(" must be written using following format: "),e("code",[t._v("2006-01-02T15:04:05")]),t._v(".")]),t._v(" "),e("p",[t._v("If the name of another field is given as a "),e("code",[t._v("date")]),t._v(", then the two fields must be a date and the field under validation must be preceding the given field.")]),t._v(" "),e("h4",{attrs:{id:"after-equal-date"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#after-equal-date"}},[t._v("#")]),t._v(" after_equal:date")]),t._v(" "),e("p",[t._v("The field under validation must be a value after or equal to the given date. The "),e("code",[t._v("date")]),t._v(" must be written using following format: "),e("code",[t._v("2006-01-02T15:04:05")]),t._v(".")]),t._v(" "),e("p",[t._v("If the name of another field is given as a "),e("code",[t._v("date")]),t._v(", then the two fields must be a date and the field under validation must be after or equal to given field.")]),t._v(" "),e("h4",{attrs:{id:"date-equals-date"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#date-equals-date"}},[t._v("#")]),t._v(" date_equals:date")]),t._v(" "),e("p",[t._v("The field under validation must be a value equal to the given date. The "),e("code",[t._v("date")]),t._v(" must be written using following format: "),e("code",[t._v("2006-01-02T15:04:05")]),t._v(".")]),t._v(" "),e("p",[t._v("If the name of another field is given as a "),e("code",[t._v("date")]),t._v(", then the two fields must be a date and the field under validation must be equal to given field.")]),t._v(" "),e("h4",{attrs:{id:"date-between-date1-date2"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#date-between-date1-date2"}},[t._v("#")]),t._v(" date_between:date1,date2")]),t._v(" "),e("p",[t._v("The field under validation must be a value between or equal to the given dates. The given dates must be written using following format: "),e("code",[t._v("2006-01-02T15:04:05")]),t._v(".")]),t._v(" "),e("p",[t._v("If the name of another field is given as a date, then all the fields must be a date and the field under validation must be between or equal to given fields.")]),t._v(" "),e("h2",{attrs:{id:"custom-rules"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#custom-rules"}},[t._v("#")]),t._v(" Custom rules")]),t._v(" "),e("p",[t._v("If none of the available validation rules satisfy your needs, you can implement custom validation rules. To do so, create a new file "),e("code",[t._v("http/validation/validation.go")]),t._v(" in which you are going to define your custom rules.")]),t._v(" "),e("p",[t._v("Rules definition shouldn't be exported, and start with "),e("code",[t._v("validate")]),t._v(". A rule returns a "),e("code",[t._v("bool")]),t._v(", indicating if the validation passed or not.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("validateCustomFormat")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" value "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" parameters "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" form "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n str"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ok "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" value"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" ok "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The data under validation is a string")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" regexp"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("MustCompile")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("parameters"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("MatchString")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("str"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("false")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Cannot validate this field")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("ul",[e("li",[e("code",[t._v("validation.RuleFunc")]),t._v(" is an alias for "),e("code",[t._v("func(string, interface{}, []string, map[string]interface{}) bool")])]),t._v(" "),e("li",[t._v("The "),e("code",[t._v("form")]),t._v(" parameter lets you access the whole form data, and modify it if needed.")]),t._v(" "),e("li",[t._v("The custom rule in the example above validates a string using a regex. If you need this kind of validation, prefer the included "),e("code",[t._v("regex")]),t._v(" validation rule.")])])]),t._v(" "),e("p",[t._v("Now that your rule behavior is defined, you need to "),e("strong",[t._v("register")]),t._v(" your rule. Do this in an "),e("code",[t._v("init()")]),t._v(" function in your "),e("code",[t._v("validation.go")]),t._v(" file.")]),t._v(" "),e("h4",{attrs:{id:"validation-addrule"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#validation-addrule"}},[t._v("#")]),t._v(" validation.AddRule")]),t._v(" "),e("p",[t._v("Register a validation rule.")]),t._v(" "),e("p",[t._v("The rule will be usable in request validation by using the given rule name.")]),t._v(" "),e("p",[t._v('Type-dependent messages let you define a different message for numeric, string, arrays and files. The language entry used will be "validation.rules.rulename.type"')]),t._v(" "),e("table",[e("thead",[e("tr",[e("th",[t._v("Parameters")]),t._v(" "),e("th",[t._v("Return")])])]),t._v(" "),e("tbody",[e("tr",[e("td",[e("code",[t._v("name string")])]),t._v(" "),e("td",[e("code",[t._v("void")])])]),t._v(" "),e("tr",[e("td",[e("code",[t._v("rule *validation.RuleDefinition")])]),t._v(" "),e("td")])])]),t._v(" "),e("p",[e("strong",[t._v("Example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("init")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("AddRule")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"custom_format"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RuleDefinition"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n Function"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" validateCustomFormat"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n RequiredParameters"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Ensure the rule has at least one parameter")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("The "),e("strong",[t._v("RuleDefinition")]),t._v(" struct is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" RuleDefinition "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The Function field is the function that will be executed")]),t._v("\n\tFunction RuleFunc\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The minimum amount of parameters")]),t._v("\n\tRequiredParameters "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// A type rule is a rule that checks if a field has a certain type")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// and can convert the raw value to a value fitting. For example, the UUID")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// rule is a type rule because it takes a string as input, checks if it's a")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// valid UUID and converts it to a "uuid.UUID".')]),t._v("\n\tIsType "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Type-dependent rules are rules that can be used with different field types")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// (numeric, string, arrays and files) and have a different validation messages")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// depending on the type.")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// The language entry used will be "validation.rules.rulename.type"')]),t._v("\n\tIsTypeDependent "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h3",{attrs:{id:"adding-a-message-to-your-rule"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#adding-a-message-to-your-rule"}},[t._v("#")]),t._v(" Adding a message to your rule")]),t._v(" "),e("p",[t._v("Finally, you may want to add a custom validation message for your rule so the client knows what's wrong with its request. Open "),e("code",[t._v("resources/lang/en-US/rules.json")]),t._v(" and add an entry with the name of your rule as key:")]),t._v(" "),e("div",{staticClass:"language-json extra-class"},[e("pre",{pre:!0,attrs:{class:"language-json"}},[e("code",[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token property"}},[t._v('"custom_format"')]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"The :field format is invalid."')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("If you are supporting multiple languages, don't forget to add this to your other "),e("code",[t._v("rules.json")]),t._v(" files too.")]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("Learn more about validation rules messages in the "),e("RouterLink",{attrs:{to:"/guide/advanced/localization.html#rules"}},[t._v("Localization documentation")]),t._v(".")],1)]),t._v(" "),e("h4",{attrs:{id:"validation-getfieldtype"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#validation-getfieldtype"}},[t._v("#")]),t._v(" validation.GetFieldType")]),t._v(" "),e("p",[e("Badge",{attrs:{text:"Since v2.0.0"}})],1),t._v(" "),e("p",[t._v("returns the non-technical type of the given "),e("code",[t._v("value")]),t._v(" interface.\nThis is used by validation rules to know if the input data is a candidate\nfor validation or not and is especially useful for type-dependent rules.")]),t._v(" "),e("ul",[e("li",[e("code",[t._v("numeric")]),t._v(" if the value is an int, uint or a float")]),t._v(" "),e("li",[e("code",[t._v("string")]),t._v(" if the value is a string")]),t._v(" "),e("li",[e("code",[t._v("array")]),t._v(" if the value is a slice")]),t._v(" "),e("li",[e("code",[t._v("file")]),t._v(" if the value is a slice of "),e("code",[t._v("filesystem.File")])]),t._v(" "),e("li",[e("code",[t._v("unsupported")]),t._v(" otherwise")])]),t._v(" "),e("table",[e("thead",[e("tr",[e("th",[t._v("Parameters")]),t._v(" "),e("th",[t._v("Return")])])]),t._v(" "),e("tbody",[e("tr",[e("td",[e("code",[t._v("value interface{}")])]),t._v(" "),e("td",[e("code",[t._v("string")])])])])]),t._v(" "),e("p",[e("strong",[t._v("Example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetFieldType")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"foo"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// "string"')]),t._v("\nvalidation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetFieldType")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// "numeric"')]),t._v("\nvalidation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetFieldType")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("2.4")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// "numeric"')]),t._v("\nvalidation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetFieldType")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// "array"')]),t._v("\n")])])]),e("h2",{attrs:{id:"validating-arrays"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#validating-arrays"}},[t._v("#")]),t._v(" Validating arrays")]),t._v(" "),e("p",[e("Badge",{attrs:{text:"Since v2.1.0"}})],1),t._v(" "),e("p",[t._v("Validating arrays is easy. All the validation rules, "),e("strong",[t._v("except the file-related rules and the "),e("code",[t._v("confirmed")]),t._v(" rule")]),t._v(", can be applied to array values using the prefix "),e("code",[t._v(">")]),t._v(". When array values are validated, "),e("strong",[t._v("all of them")]),t._v(" must pass the validation.")]),t._v(" "),e("p",[e("strong",[t._v("Example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" arrayValidation "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RuleSet"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"array"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"array:string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"between:1,5"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('">email"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('">max:128"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("In this example, we are validating an array of one to five email addresses, which can't be longer than 128 characters.")]),t._v(" "),e("h3",{attrs:{id:"n-dimensional-arrays"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#n-dimensional-arrays"}},[t._v("#")]),t._v(" N-dimensional arrays")]),t._v(" "),e("p",[t._v("You can validate n-dimensional arrays.")]),t._v(" "),e("p",[e("strong",[t._v("Example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" arrayValidation "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" RuleSet"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"array"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"array"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('">array"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('">>array:numeric"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('">max:3"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('">>>max:4"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("In this example, we are validating a three-dimensional array of numeric values. The second dimension must be made of arrays with a size of 3 or less. The third dimension must contain numbers inferior or equal to 4. The following JSON input passes the validation:")]),t._v(" "),e("div",{staticClass:"language-json extra-class"},[e("pre",{pre:!0,attrs:{class:"language-json"}},[e("code",[e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token property"}},[t._v('"array"')]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0.5")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1.42")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0.6")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("4")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0.6")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1.43")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"placeholders"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#placeholders"}},[t._v("#")]),t._v(" Placeholders")]),t._v(" "),e("p",[t._v("Validation messages can use placeholders to inject dynamic values in the validation error message. For example, in the "),e("code",[t._v("rules.json")]),t._v(" language file:")]),t._v(" "),e("div",{staticClass:"language-json extra-class"},[e("pre",{pre:!0,attrs:{class:"language-json"}},[e("code",[e("span",{pre:!0,attrs:{class:"token property"}},[t._v('"between.string"')]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"The :field must be between :min and :max characters."')]),t._v("\n")])])]),e("p",[t._v("Here, the "),e("code",[t._v(":field")]),t._v(" placeholder will be replaced by the field name, "),e("code",[t._v(":min")]),t._v(" with the first parameter and "),e("code",[t._v(":max")]),t._v(" with the second parameter, effectively giving the following result:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("The password must be between 6 and 32 characters. \n")])])]),e("p",[t._v("Placeholders are "),e("strong",[t._v("replacer functions")]),t._v(". In fact, "),e("code",[t._v("validation.Placeholder")]),t._v(" is an alias for "),e("code",[t._v("func(string, string, []string, string) string")]),t._v(". These functions should return the value to replace the placeholder with.")]),t._v(" "),e("p",[e("strong",[t._v("Example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("simpleParameterPlaceholder")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" rule "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" parameters "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" language "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" parameters"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("hr"),t._v(" "),e("p",[t._v("Placeholders are implemented in the "),e("code",[t._v("http/validation/placeholder.go")]),t._v(". To register a custom placeholder, use the "),e("code",[t._v("validation.SetPlaceholder()")]),t._v(" function, preferably in the "),e("code",[t._v("init()")]),t._v(" function of your "),e("code",[t._v("placeholder.go")]),t._v(" file.")]),t._v(" "),e("h4",{attrs:{id:"validation-setplaceholder"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#validation-setplaceholder"}},[t._v("#")]),t._v(" validation.SetPlaceholder")]),t._v(" "),e("p",[t._v("Sets the replacer function for the given placeholder. Don't include the colon prefix in the placeholder name.")]),t._v(" "),e("p",[t._v("If a placeholder with this name already exists, the latter will be overridden.")]),t._v(" "),e("table",[e("thead",[e("tr",[e("th",[t._v("Parameters")]),t._v(" "),e("th",[t._v("Return")])])]),t._v(" "),e("tbody",[e("tr",[e("td",[e("code",[t._v("placeholderName string")])]),t._v(" "),e("td",[e("code",[t._v("void")])])]),t._v(" "),e("tr",[e("td",[e("code",[t._v("replacer validation.Placeholder")])]),t._v(" "),e("td")])])]),t._v(" "),e("p",[e("strong",[t._v("Example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("SetPlaceholder")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"min"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" rule "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" parameters "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" language "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n \t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" parameters"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// Replace ":min" by the first parameter in the rule definition')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),e("h3",{attrs:{id:"available-placeholders"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#available-placeholders"}},[t._v("#")]),t._v(" Available placeholders")]),t._v(" "),e("h4",{attrs:{id:"field"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#field"}},[t._v("#")]),t._v(" :field")]),t._v(" "),e("p",[e("code",[t._v(":field")]),t._v(" is replaced by the name of the field. If it exists, the replacer with favor the language lines in "),e("code",[t._v("fields.json")]),t._v(".")]),t._v(" "),e("h4",{attrs:{id:"other"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#other"}},[t._v("#")]),t._v(" :other")]),t._v(" "),e("p",[e("code",[t._v(":other")]),t._v(" is replaced by the name of the field given as first parameter in the rule definition. If it exists, the replacer with favor the language lines in "),e("code",[t._v("fields.json")]),t._v(".")]),t._v(" "),e("p",[t._v("For example, the "),e("code",[t._v("same:password_confirmation")]),t._v(" rule compares two fields together and returns the following message if the validation fails:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("The :field and the :other must match.\n\nThe password and the password confirmation must match.\n")])])]),e("h4",{attrs:{id:"value"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#value"}},[t._v("#")]),t._v(" :value")]),t._v(" "),e("p",[e("code",[t._v(":value")]),t._v(" is replaced by the first parameter of the rule definition.")]),t._v(" "),e("h4",{attrs:{id:"values"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#values"}},[t._v("#")]),t._v(" :values")]),t._v(" "),e("p",[e("code",[t._v(":values")]),t._v(" is replaced by a concatenation of all rule parameters, joined by a comma.")]),t._v(" "),e("h4",{attrs:{id:"min"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#min"}},[t._v("#")]),t._v(" :min")]),t._v(" "),e("p",[e("code",[t._v(":min")]),t._v(" is replaced by the first parameter of the rule definition.")]),t._v(" "),e("h4",{attrs:{id:"max"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#max"}},[t._v("#")]),t._v(" :max")]),t._v(" "),e("p",[e("code",[t._v(":max")]),t._v(" is replaced by the first parameter of the rule definition, or the second if the rule name contains "),e("code",[t._v("between")]),t._v(".")]),t._v(" "),e("h4",{attrs:{id:"version"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#version"}},[t._v("#")]),t._v(" :version")]),t._v(" "),e("p",[e("code",[t._v(":version")]),t._v(" is replaced by a concatenation of "),e("code",[t._v("v")]),t._v(" and the first parameter of the rule definition, or by an empty string if the rule doesn't have any parameter.")]),t._v(" "),e("p",[t._v("For example, for the "),e("code",[t._v("UUID:4")]),t._v(" rule, the result would be "),e("code",[t._v("v4")]),t._v(".")]),t._v(" "),e("h4",{attrs:{id:"date"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#date"}},[t._v("#")]),t._v(" :date")]),t._v(" "),e("p",[e("code",[t._v(":date")]),t._v(" is replaced by the first parameter of the rule definition. If the first parameter is a field name, "),e("code",[t._v(":date")]),t._v(" will be replaced with the name of the field in the same way as the "),e("code",[t._v(":other")]),t._v(" placeholder.")]),t._v(" "),e("h4",{attrs:{id:"max-date"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#max-date"}},[t._v("#")]),t._v(" :max_date")]),t._v(" "),e("p",[e("code",[t._v(":max_date")]),t._v(" is replaced by the second parameter of the rule definition. If the second parameter is a field name, "),e("code",[t._v(":max_date")]),t._v(" will be replaced with the name of the field in the same way as the "),e("code",[t._v(":other")]),t._v(" placeholder.")]),t._v(" "),e("h2",{attrs:{id:"manual-validation"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#manual-validation"}},[t._v("#")]),t._v(" Manual validation")]),t._v(" "),e("p",[e("Badge",{attrs:{text:"Since v2.1.0"}})],1),t._v(" "),e("p",[t._v("You may need to validate some data manually as part of your business logic. You can use the Goyave validator to do so.")]),t._v(" "),e("h4",{attrs:{id:"validation-validate"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#validation-validate"}},[t._v("#")]),t._v(" validation.Validate")]),t._v(" "),e("p",[t._v("Validate the given data with the given rule set. If all validation rules pass, returns an empty "),e("code",[t._v("validation.Errors")]),t._v(".")]),t._v(" "),e("p",[t._v("The third parameter ("),e("code",[t._v("isJSON")]),t._v(") tells the function if the data comes from a JSON request. This is used to return the correct message if the given data is "),e("code",[t._v("nil")]),t._v(" and to correctly handle arrays in url-encoded requests.")]),t._v(" "),e("p",[t._v("The last parameter ("),e("code",[t._v("language")]),t._v(") sets the language of the validation error messages.")]),t._v(" "),e("table",[e("thead",[e("tr",[e("th",[t._v("Parameters")]),t._v(" "),e("th",[t._v("Return")])])]),t._v(" "),e("tbody",[e("tr",[e("td",[e("code",[t._v("data map[string]interface{}")])]),t._v(" "),e("td",[e("code",[t._v("validation.Errors")])])]),t._v(" "),e("tr",[e("td",[e("code",[t._v("rules validation.Ruler")])]),t._v(" "),e("td")]),t._v(" "),e("tr",[e("td",[e("code",[t._v("isJSON bool")])]),t._v(" "),e("td")]),t._v(" "),e("tr",[e("td",[e("code",[t._v("language string")])]),t._v(" "),e("td")])])]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("ul",[e("li",[e("code",[t._v("validation.Errors")]),t._v(" is an alias for "),e("code",[t._v("map[string][]string")]),t._v(". The key represents the field name and the associated slice contains all already translated validation error messages for this field.")]),t._v(" "),e("li",[e("code",[t._v("validation.Ruler")]),t._v(" is an interface that both "),e("code",[t._v("validation.RuleSet")]),t._v(" and "),e("code",[t._v("validation.Rules")]),t._v(" implement.")])])]),t._v(" "),e("p",[e("strong",[t._v("Example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Store")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("goyave"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Response"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" request "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("goyave"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Request"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n data "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello world"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"number"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("42")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\terrors "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Validate")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("data"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RuleSet"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"number"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"numeric"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"min:10"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("true")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" request"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Lang"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("len")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("errors"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tresponse"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("JSON")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("http"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("StatusUnprocessableEntity"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Errors"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"validationError"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" errors"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// data can be safely used from here")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ...")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"alternative-syntax"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#alternative-syntax"}},[t._v("#")]),t._v(" Alternative syntax")]),t._v(" "),e("p",[e("Badge",{attrs:{text:"Since v3.0.0"}})],1),t._v(" "),e("p",[t._v("Internally, "),e("code",[t._v("validation.RuleSet")]),t._v(" is parsed and replaced with a more complex structure the first time it is used: "),e("code",[t._v("validation.Rules")]),t._v(". This avoids having to parse rules everytime a request is received. Both "),e("code",[t._v("validation.RuleSet")]),t._v(" and "),e("code",[t._v("validation.Rules")]),t._v(" can be used when calling "),e("code",[t._v("validation.Validate()")]),t._v(", as they both implement the "),e("code",[t._v("validation.Ruler")]),t._v(" interface. The syntax for "),e("code",[t._v("validation.Rules")]),t._v(" is significantly more verbose and harder to read,")]),t._v(" "),e("p",[t._v("Here is an example of rule definition using "),e("code",[t._v("validation.Rules")]),t._v(" instead of "),e("code",[t._v("validation.RuleSet")]),t._v(":")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("rules "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Rules"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n Fields"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("FieldMap"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"email"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n Rules"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Rule"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"between"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Params"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"125"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"email"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"password"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n Rules"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Rule"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"between"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Params"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"6"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"64"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"confirmed"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"info"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n Rules"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Rule"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"nullable"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"array"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Params"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"min"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Params"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"2"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ArrayDimension"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Is the same as:")]),t._v("\nset "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" validation"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RuleSet"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"email"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"between:3,125"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"email"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"password"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"required"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"between:6,64"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"confirmed"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"info"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"nullable"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"array:string"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('">min:2"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("ul",[e("li",[e("code",[t._v("validation.FieldMap")]),t._v(" is an alias for "),e("code",[t._v("map[string]*validation.Field")])]),t._v(" "),e("li",[t._v("You can use this syntax if you need commas to be part of the values of a rule parameters.")])])])])}),[],!1,null,null,null);a.default=n.exports}}]);