package jsonschema import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestValidateInstanceAdditionalPropertiesPermitted(t *testing.T) { instance := map[string]any{ "int_val": 1, "float_val": 1.0, "bool_val": false, "an_additional_property": "abc", } schema, err := Load("./testdata/instance-validate/test-schema.json") require.NoError(t, err) err = schema.validateAdditionalProperties(instance) assert.NoError(t, err) err = schema.ValidateInstance(instance) assert.NoError(t, err) } func TestValidateInstanceAdditionalPropertiesForbidden(t *testing.T) { instance := map[string]any{ "int_val": 1, "float_val": 1.0, "bool_val": false, "an_additional_property": "abc", } schema, err := Load("./testdata/instance-validate/test-schema-no-additional-properties.json") require.NoError(t, err) err = schema.validateAdditionalProperties(instance) assert.EqualError(t, err, "property an_additional_property is not defined in the schema") err = schema.ValidateInstance(instance) assert.EqualError(t, err, "property an_additional_property is not defined in the schema") instanceWOAdditionalProperties := map[string]any{ "int_val": 1, "float_val": 1.0, "bool_val": false, } err = schema.validateAdditionalProperties(instanceWOAdditionalProperties) assert.NoError(t, err) err = schema.ValidateInstance(instanceWOAdditionalProperties) assert.NoError(t, err) } func TestValidateInstanceTypes(t *testing.T) { schema, err := Load("./testdata/instance-validate/test-schema.json") require.NoError(t, err) validInstance := map[string]any{ "int_val": 1, "float_val": 1.0, "bool_val": false, } err = schema.validateTypes(validInstance) assert.NoError(t, err) err = schema.ValidateInstance(validInstance) assert.NoError(t, err) invalidInstance := map[string]any{ "int_val": "abc", "float_val": 1.0, "bool_val": false, } err = schema.validateTypes(invalidInstance) assert.EqualError(t, err, "incorrect type for property int_val: expected type integer, but value is \"abc\"") err = schema.ValidateInstance(invalidInstance) assert.EqualError(t, err, "incorrect type for property int_val: expected type integer, but value is \"abc\"") } func TestValidateInstanceRequired(t *testing.T) { schema, err := Load("./testdata/instance-validate/test-schema-some-fields-required.json") require.NoError(t, err) validInstance := map[string]any{ "int_val": 1, "float_val": 1.0, "bool_val": false, } err = schema.validateRequired(validInstance) assert.NoError(t, err) err = schema.ValidateInstance(validInstance) assert.NoError(t, err) invalidInstance := map[string]any{ "string_val": "abc", "float_val": 1.0, "bool_val": false, } err = schema.validateRequired(invalidInstance) assert.EqualError(t, err, "no value provided for required property int_val") err = schema.ValidateInstance(invalidInstance) assert.EqualError(t, err, "no value provided for required property int_val") } func TestLoadInstance(t *testing.T) { schema, err := Load("./testdata/instance-validate/test-schema.json") require.NoError(t, err) // Expect the instance to be loaded successfully. instance, err := schema.LoadInstance("./testdata/instance-load/valid-instance.json") assert.NoError(t, err) assert.Equal(t, map[string]any{ "bool_val": false, "int_val": int64(1), "string_val": "abc", "float_val": 2.0, }, instance) // Expect instance validation against the schema to fail. _, err = schema.LoadInstance("./testdata/instance-load/invalid-type-instance.json") assert.EqualError(t, err, "incorrect type for property string_val: expected type string, but value is 123") } func TestValidateInstanceEnum(t *testing.T) { schema, err := Load("./testdata/instance-validate/test-schema-enum.json") require.NoError(t, err) validInstance := map[string]any{ "foo": "b", "bar": int64(6), } assert.NoError(t, schema.validateEnum(validInstance)) assert.NoError(t, schema.ValidateInstance(validInstance)) invalidStringInstance := map[string]any{ "foo": "d", "bar": int64(2), } assert.EqualError(t, schema.validateEnum(invalidStringInstance), "expected value of property foo to be one of [a b c]. Found: d") assert.EqualError(t, schema.ValidateInstance(invalidStringInstance), "expected value of property foo to be one of [a b c]. Found: d") invalidIntInstance := map[string]any{ "foo": "a", "bar": int64(1), } assert.EqualError(t, schema.validateEnum(invalidIntInstance), "expected value of property bar to be one of [2 4 6]. Found: 1") assert.EqualError(t, schema.ValidateInstance(invalidIntInstance), "expected value of property bar to be one of [2 4 6]. Found: 1") }