Skip to content

Commit

Permalink
Addressed issue in #482
Browse files Browse the repository at this point in the history
pattern is now checked, not format. silly me!
  • Loading branch information
daveshanley committed Apr 21, 2024
1 parent 9ba4676 commit 1c37e79
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions functions/openapi/schema_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ func (st SchemaTypeCheck) validateString(schema *base.Schema, context *model.Rul
}
}

if schema.Value.Format != "" {
if schema.Value.Pattern != "" {
vm := goja.New()
script := strings.Replace("const regex = new RegExp('{format}');", "{format}", schema.Value.Format, 1)
script := strings.Replace("const regex = new RegExp('{pattern}');", "{pattern}", schema.Value.Pattern, 1)
_, err := vm.RunString(script)
if err != nil {
result := st.buildResult("schema `format` should be a ECMA-262 regular expression dialect",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "format"),
schema, schema.Value.GoLow().Format.KeyNode, context)
result := st.buildResult("schema `pattern` should be a ECMA-262 regular expression dialect",
fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "pattern"),
schema, schema.Value.GoLow().Pattern.KeyNode, context)
results = append(results, result)
}
}
Expand Down
12 changes: 6 additions & 6 deletions functions/openapi/schema_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ components:
assert.Equal(t, "$.components.schemas['Gum'].maxLength", res[0].Path)
}

func TestSchemaType_InvalidFormat(t *testing.T) {
func TestSchemaType_InvalidPattern(t *testing.T) {

yml := `openapi: 3.1
components:
schemas:
Gum:
type: string
format: (*&@(*&@(*&@#*&@`
pattern: (*&@(*&@(*&@#*&@`

document, err := libopenapi.NewDocument([]byte(yml))
if err != nil {
Expand All @@ -178,18 +178,18 @@ components:
res := def.RunRule(nil, ctx)

assert.Len(t, res, 1)
assert.Equal(t, "schema `format` should be a ECMA-262 regular expression dialect", res[0].Message)
assert.Equal(t, "$.components.schemas['Gum'].format", res[0].Path)
assert.Equal(t, "schema `pattern` should be a ECMA-262 regular expression dialect", res[0].Message)
assert.Equal(t, "$.components.schemas['Gum'].pattern", res[0].Path)
}

func TestSchemaType_ValidFormat(t *testing.T) {
func TestSchemaType_ValidPattern(t *testing.T) {

yml := `openapi: 3.1
components:
schemas:
Gum:
type: string
format: hello`
pattern: hello`

document, err := libopenapi.NewDocument([]byte(yml))
if err != nil {
Expand Down

0 comments on commit 1c37e79

Please sign in to comment.