| 44 | } |
| 45 | |
| 46 | func TestRequired(t *testing.T) { |
| 47 | att := &expr.AttributeExpr{ |
| 48 | Type: &expr.UserTypeExpr{ |
| 49 | AttributeExpr: &expr.AttributeExpr{ |
| 50 | Type: &expr.Object{ |
| 51 | {Name: "foo", Attribute: &expr.AttributeExpr{Type: String}}, |
| 52 | {Name: "bar", Attribute: &expr.AttributeExpr{Type: String}}, |
| 53 | }, |
| 54 | }, |
| 55 | TypeName: "Foo", |
| 56 | }, |
| 57 | } |
| 58 | eval.Context = &eval.DSLContext{} |
| 59 | eval.Execute(func() { Required("foo") }, att) |
| 60 | if eval.Context.Errors != nil { |
| 61 | t.Errorf("Required failed unexpectedly with %s", eval.Context.Errors) |
| 62 | return |
| 63 | } |
| 64 | if len(att.Validation.Required) == 0 { |
| 65 | t.Errorf("Required not set on %+v", att) |
| 66 | return |
| 67 | } |
| 68 | if att.Validation.Required[0] != "foo" { |
| 69 | t.Errorf("Required invalid on %+v, expected foo, got %+v", att, att.Validation.Required) |
| 70 | } |
| 71 | uattr := att.Type.(*expr.UserTypeExpr).AttributeExpr |
| 72 | if uattr.Validation == nil { |
| 73 | t.Fatalf("Required not set on %+v", uattr) |
| 74 | } |
| 75 | if len(uattr.Validation.Required) == 0 { |
| 76 | t.Errorf("Required not set on %+v, got %+v", uattr, uattr.Validation.Required) |
| 77 | } |
| 78 | if uattr.Validation.Required[0] != "foo" { |
| 79 | t.Errorf("Required invalid on %+v, expected foo, got %+v", uattr, uattr.Validation.Required) |
| 80 | } |
| 81 | } |