fix(09): WR-04 apply a form field's required flag only in the contexts that can supply it
This commit is contained in:
@@ -608,3 +608,32 @@ func TestIsJSONScalar(t *testing.T) {
|
||||
type scalarAsArray string
|
||||
|
||||
func (s scalarAsArray) MarshalJSON() ([]byte, error) { return json.Marshal([]string{string(s)}) }
|
||||
|
||||
// TestCRUDRequiredFollowsContext pins WR-04: a form field that is `required`
|
||||
// but limited to the update context cannot be supplied on create, so it must
|
||||
// not make every create fail; it still binds on update.
|
||||
func TestCRUDRequiredFollowsContext(t *testing.T) {
|
||||
svc, cc, db := crudFixture(t)
|
||||
for i := range cc.Form.Fields {
|
||||
if cc.Form.Fields[i].Name == "note" {
|
||||
cc.Form.Fields[i].Required = true
|
||||
cc.Form.Fields[i].Context = &fieldContext{values: []string{"update"}}
|
||||
}
|
||||
}
|
||||
created, err := svc.CreateRecord(context.Background(), cc, RecordInput{Body: crudBody(t, `{"name":"Ada"}`)})
|
||||
if err != nil {
|
||||
t.Fatalf("create with an update-only required field: %v", err)
|
||||
}
|
||||
id := created.Data["id"]
|
||||
if _, err := svc.UpdateRecord(context.Background(), cc, id, RecordInput{Body: crudBody(t, `{"name":"Bea"}`)}); err == nil {
|
||||
t.Fatal("update without the required update-context field succeeded")
|
||||
} else {
|
||||
assertValidation(t, err, "note", "The note field is required.")
|
||||
}
|
||||
if _, err := svc.UpdateRecord(context.Background(), cc, id, RecordInput{Body: crudBody(t, `{"name":"Bea","note":"n"}`)}); err != nil {
|
||||
t.Fatalf("update with the field: %v", err)
|
||||
}
|
||||
if row := loadCrud(t, db, "Bea"); row.Note != "n" {
|
||||
t.Fatalf("row=%+v", row)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user