diff --git a/schema/core.go b/schema/core.go index 1119201..ef6a00b 100644 --- a/schema/core.go +++ b/schema/core.go @@ -291,6 +291,30 @@ func (a CoreAttribute) ValidateSingular(attribute interface{}) (interface{}, *er } } +// WithDescription returns a copy of the attribute with the given description. +func (a CoreAttribute) WithDescription(description optional.String) CoreAttribute { + a.description = description + return a +} + +// WithMutability returns a copy of the attribute with the given mutability. +func (a CoreAttribute) WithMutability(mutability AttributeMutability) CoreAttribute { + a.mutability = mutability.m + return a +} + +// WithRequired returns a copy of the attribute with the given required value. +func (a CoreAttribute) WithRequired(required bool) CoreAttribute { + a.required = required + return a +} + +// WithReturned returns a copy of the attribute with the given returned value. +func (a CoreAttribute) WithReturned(returned AttributeReturned) CoreAttribute { + a.returned = returned.r + return a +} + func (a *CoreAttribute) getRawAttributes() map[string]interface{} { attributes := map[string]interface{}{ "description": a.description.Value(), diff --git a/schema/core_test.go b/schema/core_test.go new file mode 100644 index 0000000..8fdbeff --- /dev/null +++ b/schema/core_test.go @@ -0,0 +1,75 @@ +package schema + +import ( + "fmt" + "testing" + + "github.com/elimity-com/scim/optional" +) + +func ExampleCoreAttribute_WithRequired() { + // Customize the pre-built CoreUserSchema to make "emails" required. + userSchema := CoreUserSchema() + for i, attr := range userSchema.Attributes { + if attr.Name() == "emails" { + userSchema.Attributes[i] = attr.WithRequired(true) + } + } + + emails, _ := userSchema.Attributes.ContainsAttribute("emails") + fmt.Println(emails.Required()) + // Output: true +} + +func TestCoreAttribute_WithDescription(t *testing.T) { + attr := SimpleCoreAttribute(SimpleStringParams(StringParams{ + Name: "test", + })) + desc := optional.NewString("new description") + got := attr.WithDescription(desc) + if got.Description() != "new description" { + t.Errorf("WithDescription: got %q, want %q", got.Description(), "new description") + } + if attr.Description() != "" { + t.Error("WithDescription modified the original attribute") + } +} + +func TestCoreAttribute_WithMutability(t *testing.T) { + attr := SimpleCoreAttribute(SimpleStringParams(StringParams{ + Name: "test", + })) + got := attr.WithMutability(AttributeMutabilityImmutable()) + if got.Mutability() != "immutable" { + t.Errorf("WithMutability: got %q, want %q", got.Mutability(), "immutable") + } + if attr.Mutability() != "readWrite" { + t.Error("WithMutability modified the original attribute") + } +} + +func TestCoreAttribute_WithRequired(t *testing.T) { + attr := SimpleCoreAttribute(SimpleStringParams(StringParams{ + Name: "test", + })) + got := attr.WithRequired(true) + if !got.Required() { + t.Error("WithRequired(true): got false, want true") + } + if attr.Required() { + t.Error("WithRequired modified the original attribute") + } +} + +func TestCoreAttribute_WithReturned(t *testing.T) { + attr := SimpleCoreAttribute(SimpleStringParams(StringParams{ + Name: "test", + })) + got := attr.WithReturned(AttributeReturnedNever()) + if got.Returned() != "never" { + t.Errorf("WithReturned: got %q, want %q", got.Returned(), "never") + } + if attr.Returned() != "default" { + t.Error("WithReturned modified the original attribute") + } +}