Skip to content
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
24 changes: 24 additions & 0 deletions schema/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
75 changes: 75 additions & 0 deletions schema/core_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
Loading