Maximum adds a "maximum" validation to the attribute. See http://json-schema.org/latest/json-schema-validation.html#anchor17. Example: Attribute("integer", Int, func() { Maximum(100) })
(val any)
| 317 | // Maximum(100) |
| 318 | // }) |
| 319 | func Maximum(val any) { |
| 320 | if a, ok := eval.Current().(*expr.AttributeExpr); ok { |
| 321 | if a.Type != nil && |
| 322 | a.Type.Kind() != expr.IntKind && a.Type.Kind() != expr.UIntKind && |
| 323 | a.Type.Kind() != expr.Int32Kind && a.Type.Kind() != expr.UInt32Kind && |
| 324 | a.Type.Kind() != expr.Int64Kind && a.Type.Kind() != expr.UInt64Kind && |
| 325 | a.Type.Kind() != expr.Float32Kind && a.Type.Kind() != expr.Float64Kind { |
| 326 | incompatibleAttributeType("maximum", a.Type.Name(), "an integer or a number") |
| 327 | } else { |
| 328 | var f float64 |
| 329 | switch v := val.(type) { |
| 330 | case float32, float64, int, int8, int16, int32, int64, uint8, uint16, uint32, uint64: |
| 331 | f = reflect.ValueOf(v).Convert(reflect.TypeOf(float64(0.0))).Float() |
| 332 | case string: |
| 333 | var err error |
| 334 | f, err = strconv.ParseFloat(v, 64) |
| 335 | if err != nil { |
| 336 | eval.ReportError("invalid number value %#v", v) |
| 337 | return |
| 338 | } |
| 339 | default: |
| 340 | eval.ReportError("invalid number value %#v", v) |
| 341 | return |
| 342 | } |
| 343 | if a.Validation == nil { |
| 344 | a.Validation = &expr.ValidationExpr{} |
| 345 | } |
| 346 | a.Validation.Maximum = &f |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // MinLength adds a "minItems" validation to the attribute. |
| 352 | // See http://json-schema.org/latest/json-schema-validation.html#anchor45. |