Apply comments

This commit is contained in:
Arpit Jasapara 2024-01-21 22:52:31 -08:00
parent 5a37d661a7
commit f31191fd2a
No known key found for this signature in database
GPG Key ID: F10F7691DDA7C0B9
3 changed files with 24 additions and 12 deletions

View File

@ -138,20 +138,22 @@ func (s *Schema) validateConst(instance map[string]any) error {
continue
}
v, ok := instance[name]
if !ok {
return fmt.Errorf("property %s has const set to %v but no value was provided", name, property.Const)
}
if v != property.Const {
if ok && v != property.Const {
return fmt.Errorf("expected value of property %s to be %v. Found: %v", name, property.Const, v)
}
}
return nil
}
// Validates that the instance matches at least one of the schemas in anyOf
// but will also succeed if the property values are omitted.
// For more information, see https://json-schema.org/understanding-json-schema/reference/combining#anyof.
func (s *Schema) validateAnyOf(instance map[string]any) error {
if s.AnyOf == nil {
return nil
}
// Currently, we only validate const for anyOf schemas since anyOf is
// only used by skip_prompt_if, which only supports const.
for _, anyOf := range s.AnyOf {
err := anyOf.validateConst(instance)
if err == nil {

View File

@ -237,15 +237,15 @@ func TestValidateInstanceForConst(t *testing.T) {
// Empty instance
emptyInstanceValue := map[string]any{}
assert.ErrorContains(t, schema.validateConst(emptyInstanceValue), "but no value was provided")
assert.ErrorContains(t, schema.ValidateInstance(emptyInstanceValue), "but no value was provided")
assert.NoError(t, schema.validateConst(emptyInstanceValue))
assert.NoError(t, schema.ValidateInstance(emptyInstanceValue))
// Missing value for bar
missingInstanceValue := map[string]any{
"foo": "abc",
}
assert.EqualError(t, schema.validateConst(missingInstanceValue), "property bar has const set to def but no value was provided")
assert.EqualError(t, schema.ValidateInstance(missingInstanceValue), "property bar has const set to def but no value was provided")
assert.NoError(t, schema.validateConst(missingInstanceValue))
assert.NoError(t, schema.ValidateInstance(missingInstanceValue))
// Valid value for bar, invalid value for foo
invalidInstanceValue := map[string]any{
@ -286,15 +286,15 @@ func TestValidateInstanceForAnyOf(t *testing.T) {
// Empty instance
emptyInstanceValue := map[string]any{}
assert.EqualError(t, schema.validateAnyOf(emptyInstanceValue), "instance does not match any of the schemas in anyOf")
assert.EqualError(t, schema.ValidateInstance(emptyInstanceValue), "instance does not match any of the schemas in anyOf")
assert.NoError(t, schema.validateAnyOf(emptyInstanceValue))
assert.NoError(t, schema.ValidateInstance(emptyInstanceValue))
// Missing values for bar, invalid value for foo
missingInstanceValue := map[string]any{
"foo": "xyz",
}
assert.EqualError(t, schema.validateAnyOf(missingInstanceValue), "instance does not match any of the schemas in anyOf")
assert.EqualError(t, schema.ValidateInstance(missingInstanceValue), "instance does not match any of the schemas in anyOf")
assert.NoError(t, schema.validateAnyOf(missingInstanceValue))
assert.NoError(t, schema.ValidateInstance(missingInstanceValue))
// Valid value for bar, invalid value for foo
invalidInstanceValue := map[string]any{

View File

@ -461,6 +461,16 @@ func TestPromptIsSkippedAnyOf(t *testing.T) {
assert.False(t, skip)
assert.NotContains(t, c.values, "xyz")
// Missing values. Prompt should not be skipped.
c.values["abc"] = "foobar"
skip, err = c.skipPrompt(jsonschema.Property{
Name: "xyz",
Schema: c.schema.Properties["xyz"],
}, testRenderer())
assert.NoError(t, err)
assert.False(t, skip)
assert.NotContains(t, c.values, "xyz")
// Values match skip condition. Prompt should be skipped. Default value should
// be assigned to "xyz".
c.values["abc"] = "foobar"