Skip to content

Commit

Permalink
Remove validation for default value against pattern (#959)
Browse files Browse the repository at this point in the history
## Changes
This PR removes validation for default value against the regex pattern
specified in a JSON schema at schema load time. This is required because
#795 introduces parameterising the
default value as a Go text template impling that the default value now
does not necessarily have to match the pattern at schema load time.

This will also unblock:
databricks/mlops-stacks#108

Note, this does not remove runtime validation for input parameters right
before template initialization, which happens here:
https://github.com/databricks/cli/blob/fb32e78c9b9fb000ce898b8a60b0b47920f487d3/libs/template/materialize.go#L76

## Tests
Changes to existing test.
  • Loading branch information
shreyas-goenka authored Nov 7, 2023
1 parent 677f28e commit 283f241
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 17 deletions.
5 changes: 0 additions & 5 deletions libs/jsonschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,6 @@ func (schema *Schema) validateSchemaPattern() error {
return fmt.Errorf("invalid regex pattern %q provided for property %q: %w", pattern, name, err)
}

// validate default value against the pattern
if property.Default != nil && !r.MatchString(property.Default.(string)) {
return fmt.Errorf("default value %q for property %q does not match specified regex pattern: %q", property.Default, name, pattern)
}

// validate enum values against the pattern
for i, enum := range property.Enum {
if !r.MatchString(enum.(string)) {
Expand Down
13 changes: 1 addition & 12 deletions libs/jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestSchemaValidateIncorrectRegex(t *testing.T) {
assert.EqualError(t, s.validate(), "invalid regex pattern \"(abc\" provided for property \"foo\": error parsing regexp: missing closing ): `(abc`")
}

func TestSchemaValidatePatternDefault(t *testing.T) {
func TestSchemaDefaultValueIsNotValidatedAgainstPattern(t *testing.T) {
s := &Schema{
Properties: map[string]*Schema{
"foo": {
Expand All @@ -185,17 +185,6 @@ func TestSchemaValidatePatternDefault(t *testing.T) {
},
},
}
assert.EqualError(t, s.validate(), "default value \"def\" for property \"foo\" does not match specified regex pattern: \"abc\"")

s = &Schema{
Properties: map[string]*Schema{
"foo": {
Type: "string",
Pattern: "a.*d",
Default: "axyzd",
},
},
}
assert.NoError(t, s.validate())
}

Expand Down

0 comments on commit 283f241

Please sign in to comment.