Skip to content

Commit

Permalink
chore: tests and prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
davenewza committed Oct 29, 2023
1 parent f97cc88 commit d632dfd
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 46 deletions.
4 changes: 2 additions & 2 deletions schema/testdata/validation_fields_unique_in_model/errors.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"errors": [
{
"message": "You have duplicate field names name",
"hint": "Remove 'name' on line 4",
"message": "Cannot use 'name' as it has already been defined on this model",
"hint": "Rename this field to some other name which has not yet been defined",
"code": "E003",
"pos": {
"filename": "testdata/validation_fields_unique_in_model/schema.keel",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"errors": [
{
"message": "You have duplicate field names createdAt",
"hint": "Remove 'createdAt' on line 0",
"message": "Cannot use 'createdAt' as it has already been defined on this model",
"hint": "Rename this field to some other name which has not yet been defined",
"code": "E003",
"pos": {
"filename": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"errors": [
{
"message": "You have duplicate field names companyEmployee",
"hint": "Remove 'companyEmployee' on line 0",
"message": "Cannot use 'companyEmployee' as it has already been defined on this model",
"hint": "Rename this field to some other name which has not yet been defined",
"code": "E003",
"pos": {
"filename": "",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"errors": [
{
"message": "You have a reserved field name createdAt",
"hint": "Try '\u003cno value\u003e' instead",
"message": "Cannot use 'createdAt' as it already exists as a built-in field",
"hint": "Rename this field to some other name which has not yet been defined",
"code": "E006",
"pos": {
"filename": "",
"offset": 0,
"line": 0,
"column": 0
"filename": "testdata/validation_reserved_field_name_created_at/schema.keel",
"offset": 28,
"line": 3,
"column": 5
},
"endPos": {
"filename": "",
"offset": 0,
"line": 0,
"column": 0
"filename": "testdata/validation_reserved_field_name_created_at/schema.keel",
"offset": 37,
"line": 3,
"column": 14
}
}
]
Expand Down
20 changes: 10 additions & 10 deletions schema/testdata/validation_reserved_field_name_id/errors.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"errors": [
{
"message": "You have a reserved field name id",
"hint": "Try '\u003cno value\u003e' instead",
"message": "Cannot use 'id' as it already exists as a built-in field",
"hint": "Rename this field to some other name which has not yet been defined",
"code": "E006",
"pos": {
"filename": "",
"offset": 0,
"line": 0,
"column": 0
"filename": "testdata/validation_reserved_field_name_id/schema.keel",
"offset": 36,
"line": 3,
"column": 9
},
"endPos": {
"filename": "",
"offset": 0,
"line": 0,
"column": 0
"filename": "testdata/validation_reserved_field_name_id/schema.keel",
"offset": 38,
"line": 3,
"column": 11
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"errors": [
{
"message": "You have a reserved field name updatedAt",
"hint": "Try '\u003cno value\u003e' instead",
"message": "Cannot use 'updatedAt' as it already exists as a built-in field",
"hint": "Rename this field to some other name which has not yet been defined",
"code": "E006",
"pos": {
"filename": "",
"offset": 0,
"line": 0,
"column": 0
"filename": "testdata/validation_reserved_field_name_updated_at/schema.keel",
"offset": 28,
"line": 3,
"column": 5
},
"endPos": {
"filename": "",
"offset": 0,
"line": 0,
"column": 0
"filename": "testdata/validation_reserved_field_name_updated_at/schema.keel",
"offset": 37,
"line": 3,
"column": 14
}
}
]
Expand Down
6 changes: 0 additions & 6 deletions schema/validation/errorhandling/errors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ en:
E002:
message: "Action names should be written in lowerCamelCase"
hint: "Did you mean '{{ .Suggested }}'?"
E003:
message: "You have duplicate field names {{ .Name }}"
hint: "Remove '{{ .Name }}' on line {{ .Line }}"
E004:
message: "You have duplicate actions Model:{{ .Model }} Name:{{ .Name }}"
hint: "Remove '{{ .Name }}' on line {{ .Line }}"
E005:
message: "Action inputs must be one of the fields defined in the model"
hint: "{{ .Suggested }}"
E006:
message: "You have a reserved field name {{ .Name }}"
hint: "Try '{{ .Suggestion }}' instead"
E008:
message: "Action {{ .Name }} must either take a unique field as input or filter on a unique field using a @where attribute"
hint: "Did you mean to add 'id' as an input?"
Expand Down
8 changes: 4 additions & 4 deletions schema/validation/rules/field/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (

func UniqueFieldNamesRule(asts []*parser.AST) (errs errorhandling.ValidationErrors) {
for _, model := range query.Models(asts) {
fieldNames := map[string]bool{}
fieldNames := map[string]*parser.FieldNode{}
for _, field := range query.ModelFields(model) {
if _, ok := fieldNames[field.Name.Value]; ok {
if existingField, ok := fieldNames[field.Name.Value]; ok {
if field.BuiltIn {
errs.Append(errorhandling.ErrorReservedFieldName,
map[string]string{
"Name": field.Name.Value,
"Line": fmt.Sprint(field.Name.Pos.Line),
},
field.Name,
existingField.Name,
)
} else {
errs.Append(errorhandling.ErrorFieldNamesUniqueInModel,
Expand All @@ -34,7 +34,7 @@ func UniqueFieldNamesRule(asts []*parser.AST) (errs errorhandling.ValidationErro
}
}

fieldNames[field.Name.Value] = true
fieldNames[field.Name.Value] = field
}
}

Expand Down

0 comments on commit d632dfd

Please sign in to comment.