Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ref being validated in object schema #58

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions schema/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,16 @@ func (o *ObjectSchema) validateStruct(data any) error {
return o.validateFieldInterdependencies(rawData)
}

func (o *ObjectSchema) convertToObjectSchema(typeOrData any) (*ObjectSchema, bool) {
schemaType, ok := typeOrData.(*ObjectSchema)
func (o *ObjectSchema) convertToObjectSchema(typeOrData any) (Object, bool) {
// Try plain object schema
objectSchemaType, ok := typeOrData.(*ObjectSchema)
if ok {
return schemaType, true
return objectSchemaType, true
}
// Next, try ref schema
refSchemaType, ok := typeOrData.(*RefSchema)
if ok {
return refSchemaType.referencedObjectCache, true
}
// Try getting the inlined ObjectSchema for objects, like TypedObjectSchema, that do that.
value := reflect.ValueOf(typeOrData)
Expand All @@ -351,15 +357,15 @@ func (o *ObjectSchema) convertToObjectSchema(typeOrData any) (*ObjectSchema, boo
fieldAsInterface := field.Interface()
objectType, ok2 := fieldAsInterface.(ObjectSchema)
if ok2 {
schemaType = &objectType
objectSchemaType = &objectType
ok = true
}
}
}
return schemaType, ok
return objectSchemaType, ok
}

func (o *ObjectSchema) validateSchemaCompatibility(schemaType *ObjectSchema) error {
func (o *ObjectSchema) validateSchemaCompatibility(schemaType Object) error {
fieldData := map[string]any{}
// Validate IDs. This is important because the IDs should match.
if schemaType.ID() != o.ID() {
Expand Down
8 changes: 8 additions & 0 deletions schema/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,18 @@ func TestTypedObjectSchema_Any(t *testing.T) {
assert.Error(t, err)
}

var testStructScope = schema.NewScopeSchema(&testStructSchema.ObjectSchema)

func TestObjectSchema_ValidateCompatibility(t *testing.T) {
// Schema validation
assert.NoError(t, testStructSchema.ValidateCompatibility(testStructSchema))
assert.Error(t, testStructSchema.ValidateCompatibility(testOptionalFieldSchema)) // Not the same
// Schema validation with ref
objectTestRef := schema.NewRefSchema("testStruct", nil)
objectTestRef.ApplyScope(testStructScope)
assert.NoError(t, objectTestRef.ValidateCompatibility(testStructSchema))
assert.NoError(t, testStructSchema.ValidateCompatibility(objectTestRef))

// map verification
validData := map[string]any{
"Field1": 42,
Expand Down
Loading