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

Added namespaced scopes #80

Merged
merged 12 commits into from
Mar 22, 2024
5 changes: 4 additions & 1 deletion schema/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ func (a *AnySchema) Serialize(data any) (any, error) {
return a.checkAndConvert(data)
}

func (a *AnySchema) ApplyScope(_ Scope) {
func (a *AnySchema) ApplyScope(_ Scope, _ string) {}

func (a *AnySchema) ValidateReferences() error {
// No references in this type. No work to do.
return nil
}

func (a *AnySchema) TypeID() TypeID {
Expand Down
5 changes: 4 additions & 1 deletion schema/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ func (b BoolSchema) SerializeType(data bool) (any, error) {
return b.Serialize(data)
}

func (b BoolSchema) ApplyScope(scope Scope) {
func (b BoolSchema) ApplyScope(_ Scope, _ string) {}

func (b BoolSchema) ValidateReferences() error {
// No references in this type. No work to do.
return nil
}

func (b BoolSchema) TypeID() TypeID {
Expand Down
18 changes: 11 additions & 7 deletions schema/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,30 @@ func (e EnumSchema[T]) ValidValues() map[T]*DisplayValue {
return e.ValidValuesMap
}

func (e EnumSchema[T]) ApplyScope(scope Scope) {
func (e EnumSchema[T]) ApplyScope(_ Scope, _ string) {}

func (e EnumSchema[T]) ValidateReferences() error {
// No references in this type. No work to do.
return nil
}

func (e EnumSchema[T]) ReflectedType() reflect.Type {
var defaultValue T
return reflect.TypeOf(defaultValue)
}

func (s EnumSchema[T]) ValidateCompatibility(typeOrData any) error {
func (e EnumSchema[T]) ValidateCompatibility(typeOrData any) error {
// Check if it's a schema type. If it is, verify it. If not, verify it as data.
value := reflect.ValueOf(typeOrData)
if reflect.Indirect(value).Kind() != reflect.Struct {
// Validate as data
return s.Validate(typeOrData)
return e.Validate(typeOrData)
}
field := reflect.Indirect(value).FieldByName("EnumSchema")

if !field.IsValid() {
// Validate as data
return s.Validate(typeOrData)
return e.Validate(typeOrData)
}

// Validate the type of EnumSchema
Expand All @@ -55,16 +59,16 @@ func (s EnumSchema[T]) ValidateCompatibility(typeOrData any) error {
return &ConstraintError{
Message: fmt.Sprintf(
"validation failed for enum. Found type (%T) does not match expected type (%T)",
fieldAsInterface, s),
fieldAsInterface, e),
}
}

// Validate the valid values
for key, display := range s.ValidValuesMap {
for key, display := range e.ValidValuesMap {
matchingInputDisplay := schemaType.ValidValuesMap[key]
if matchingInputDisplay == nil {
foundValues := reflect.ValueOf(schemaType.ValidValuesMap).MapKeys()
expectedValues := reflect.ValueOf(s.ValidValuesMap).MapKeys()
expectedValues := reflect.ValueOf(e.ValidValuesMap).MapKeys()
return &ConstraintError{
Message: fmt.Sprintf("invalid enum values for type '%T' for custom enum. Missing key %v (and potentially others). Expected values: %s, Has values: %s",
typeOrData, key, expectedValues, foundValues),
Expand Down
6 changes: 5 additions & 1 deletion schema/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ func (f FloatSchema) Max() *float64 {
func (f FloatSchema) Units() *UnitsDefinition {
return f.UnitsValue
}
func (f FloatSchema) ApplyScope(scope Scope) {
func (f FloatSchema) ApplyScope(_ Scope, _ string) {}

func (f FloatSchema) ValidateReferences() error {
// No references in this type. No work to do.
return nil
}

func (f FloatSchema) Unserialize(data any) (any, error) {
Expand Down
6 changes: 5 additions & 1 deletion schema/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ func (i IntSchema) ReflectedType() reflect.Type {
return reflect.TypeOf(int64(0))
}

func (i IntSchema) ApplyScope(scope Scope) {
func (i IntSchema) ApplyScope(_ Scope, _ string) {}

func (i IntSchema) ValidateReferences() error {
// No references in this type. No work to do.
return nil
}

func (i IntSchema) TypeID() TypeID {
Expand Down
8 changes: 6 additions & 2 deletions schema/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ func (l AbstractListSchema[ItemType]) Max() *int64 {
return l.MaxValue
}

func (l AbstractListSchema[ItemType]) ApplyScope(scope Scope) {
l.ItemsValue.ApplyScope(scope)
func (l AbstractListSchema[ItemType]) ApplyScope(scope Scope, namespace string) {
l.ItemsValue.ApplyScope(scope, namespace)
}

func (l AbstractListSchema[ItemType]) ValidateReferences() error {
return l.ItemsValue.ValidateReferences()
}

func (l AbstractListSchema[ItemType]) ReflectedType() reflect.Type {
Expand Down
14 changes: 11 additions & 3 deletions schema/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,17 @@ func (m MapSchema[K, V]) Max() *int64 {
return m.MaxValue
}

func (m MapSchema[K, V]) ApplyScope(scope Scope) {
m.KeysValue.ApplyScope(scope)
m.ValuesValue.ApplyScope(scope)
func (m MapSchema[K, V]) ApplyScope(scope Scope, namespace string) {
m.KeysValue.ApplyScope(scope, namespace)
m.ValuesValue.ApplyScope(scope, namespace)
}

func (m MapSchema[K, V]) ValidateReferences() error {
err := m.KeysValue.ValidateReferences()
if err != nil {
return err
}
return m.ValuesValue.ValidateReferences()
}

func (m MapSchema[K, V]) Unserialize(data any) (any, error) {
Expand Down
16 changes: 13 additions & 3 deletions schema/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,22 @@ func (o *ObjectSchema) GetDefaults() map[string]any {
return o.defaultValues
}

func (o *ObjectSchema) ApplyScope(scope Scope) {
func (o *ObjectSchema) ApplyScope(scope Scope, namespace string) {
for _, property := range o.PropertiesValue {
property.ApplyScope(scope)
property.ApplyScope(scope, namespace)
}
}

func (o *ObjectSchema) ValidateReferences() error {
for _, property := range o.PropertiesValue {
err := property.ValidateReferences()
if err != nil {
return err
}
}
return nil
}

func (o *ObjectSchema) TypeID() TypeID {
return TypeIDObject
}
Expand Down Expand Up @@ -341,7 +351,7 @@ func (o *ObjectSchema) convertToObjectSchema(typeOrData any) (Object, bool) {
// Next, try ref schema
refSchemaType, ok := typeOrData.(*RefSchema)
if ok {
return refSchemaType.referencedObjectCache, true
return refSchemaType.GetObject(), true
}
// Next, try scope schema.
scopeSchemaType, ok := typeOrData.(*ScopeSchema)
webbnh marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion schema/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ func TestObjectSchema_ValidateCompatibility(t *testing.T) {
assert.Error(t, testStructSchema.ValidateCompatibility(testOptionalFieldSchema)) // Not the same
// Schema validation with ref
objectTestRef := schema.NewRefSchema("testStruct", nil)
objectTestRef.ApplyScope(testStructScope)
objectTestRef.ApplyScope(testStructScope, schema.DEFAULT_NAMESPACE)
assert.NoError(t, objectTestRef.ValidateCompatibility(testStructSchema))
assert.NoError(t, testStructSchema.ValidateCompatibility(objectTestRef))
// Schema validation with scope
Expand Down
14 changes: 12 additions & 2 deletions schema/oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func (o OneOfSchema[KeyType]) DiscriminatorFieldName() string {
return o.DiscriminatorFieldNameValue
}

func (o OneOfSchema[KeyType]) ApplyScope(scope Scope) {
func (o OneOfSchema[KeyType]) ApplyScope(scope Scope, namespace string) {
for _, t := range o.TypesValue {
t.ApplyScope(scope)
t.ApplyScope(scope, namespace)
}
// scope must be applied before we can access the subtypes' properties
err := o.validateSubtypeDiscriminatorInlineFields()
Expand All @@ -54,6 +54,16 @@ func (o OneOfSchema[KeyType]) ApplyScope(scope Scope) {
}
}

func (o OneOfSchema[KeyType]) ValidateReferences() error {
for _, t := range o.TypesValue {
err := t.ValidateReferences()
if err != nil {
return err
}
}
return nil
}

func (o OneOfSchema[KeyType]) ReflectedType() reflect.Type {
if o.interfaceType == nil {
var defaultValue any
Expand Down
2 changes: 1 addition & 1 deletion schema/oneof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func Test_OneOf_ConstructorBypass(t *testing.T) { //nolint:funlen

scopeAny := assert.NoErrorR[any](t)(schema.DescribeScope().Unserialize(input_schema))
scopeSchemaTyped := scopeAny.(*schema.ScopeSchema)
scopeSchemaTyped.ApplyScope(scopeSchemaTyped)
scopeSchemaTyped.ApplyScope(scopeSchemaTyped, schema.DEFAULT_NAMESPACE)
assert.NoError(t, scopeSchemaTyped.Validate(input_data_fullname))
unserialized := assert.NoErrorR[any](t)(scopeSchemaTyped.Unserialize(input_data_fullname))
serialized := assert.NoErrorR[any](t)(scopeSchemaTyped.Serialize(unserialized))
Expand Down
6 changes: 5 additions & 1 deletion schema/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ func (p PatternSchema) TypeID() TypeID {
return TypeIDPattern
}

func (p PatternSchema) ApplyScope(scope Scope) {
func (p PatternSchema) ApplyScope(_ Scope, _ string) {}

func (p PatternSchema) ValidateReferences() error {
// No references in this type. No work to do.
return nil
}

func (p PatternSchema) ReflectedType() reflect.Type {
Expand Down
8 changes: 6 additions & 2 deletions schema/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ func (p *PropertySchema) Examples() []string {
return p.ExamplesValue
}

func (p *PropertySchema) ApplyScope(scope Scope) {
p.TypeValue.ApplyScope(scope)
func (p *PropertySchema) ApplyScope(scope Scope, namespace string) {
p.TypeValue.ApplyScope(scope, namespace)
}

func (p *PropertySchema) ValidateReferences() error {
return p.TypeValue.ValidateReferences()
}

func (p *PropertySchema) Unserialize(data any) (any, error) {
Expand Down
Loading