MaxLength adds a "maxItems" validation to the attribute. See http://json-schema.org/latest/json-schema-validation.html#anchor42. Example: Attribute("array", ArrayOf(String), func() { MaxLength(200) // max array length Elem(func() { MaxLength(5) // max length of each array e
(val int)
| 393 | // }) |
| 394 | // }) |
| 395 | func MaxLength(val int) { |
| 396 | if a, ok := eval.Current().(*expr.AttributeExpr); ok { |
| 397 | if a.Type != nil { |
| 398 | kind := a.Type.Kind() |
| 399 | if kind != expr.BytesKind && |
| 400 | kind != expr.StringKind && |
| 401 | kind != expr.ArrayKind && |
| 402 | kind != expr.MapKind { |
| 403 | incompatibleAttributeType("maximum length", a.Type.Name(), "a string or an array") |
| 404 | return |
| 405 | } |
| 406 | } |
| 407 | if a.Validation == nil { |
| 408 | a.Validation = &expr.ValidationExpr{} |
| 409 | } |
| 410 | a.Validation.MaxLength = &val |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Required adds a "required" validation to the attribute. |
| 415 | // See http://json-schema.org/latest/json-schema-validation.html#anchor61. |