Skip to content

Commit e9e772e

Browse files
authored
Merge pull request #502 from gfrey/fix_field_parser
Fix field schema parsing
2 parents 3afca1e + 0ad6615 commit e9e772e

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

pkg/simpleschema/field.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@ func parseFieldSchema(fieldSchema string) (string, []*Marker, error) {
3636
}
3737

3838
// split the type and markers if possible
39-
parts := strings.Split(fieldSchema, "|")
40-
if len(parts) > 2 {
41-
return "", nil, fmt.Errorf("invalid type: %s", fieldSchema)
42-
}
39+
parts := strings.SplitN(fieldSchema, "|", 2)
4340

4441
// trim spaces from the type
4542
typ := strings.TrimSpace(parts[0])

pkg/simpleschema/field_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ func TestParseFieldSchema(t *testing.T) {
7272
},
7373
wantErr: false,
7474
},
75+
{
76+
name: "array field with complex validation",
77+
fieldSchema: "[]string | validation=\"self.all(x, startsWith(x, 'foo') || startsWith(x, 'bar'))\"",
78+
wantType: "[]string",
79+
wantMarkers: []*Marker{
80+
{MarkerType: MarkerTypeValidation, Key: "validation", Value: "self.all(x, startsWith(x, 'foo') || startsWith(x, 'bar'))"},
81+
},
82+
wantErr: false,
83+
},
7584
}
7685

7786
for _, tt := range tests {

0 commit comments

Comments
 (0)