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

Allow strings as numbers and booleans #165

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion internal/patch/patch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package patch

import (
"bytes"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -41,7 +42,11 @@ func NewValidator(patchReq string, s schema.Schema, extensions ...schema.Schema)
Path string
Value interface{}
}
if err := json.Unmarshal([]byte(patchReq), &operation); err != nil {

// Decode a number into a json.Number instead of floag64
d := json.NewDecoder(bytes.NewBufferString(patchReq))
d.UseNumber()
if err := d.Decode(&operation); err != nil {
return OperationValidator{}, err
}

Expand Down
91 changes: 90 additions & 1 deletion internal/patch/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package patch

import (
"fmt"
"testing"

"github.com/elimity-com/scim/schema"
"github.com/scim2/filter-parser/v2"
"testing"
)

func TestNewPathValidator(t *testing.T) {
Expand All @@ -31,6 +32,94 @@ func TestNewPathValidator(t *testing.T) {
t.Error("expected JSON error, got none")
}
})
t.Run("Valid integer", func(t *testing.T) {
ops := []string{
`{"op":"add","path":"attr2","value":1234}`,
`{"op":"add","path":"attr2","value":"1234"}`,
}
for _, op := range ops {
validator, err := NewValidator(op, patchSchema)
if err != nil {
t.Errorf("unexpected error, got %v", err)
return
}
v, err := validator.Validate()
if err != nil {
t.Errorf("unexpected error, got %v", err)
return
}
n, ok := v.(int64)
if !ok {
t.Errorf("unexpected type, got %T", v)
return
}
if n != 1234 {
t.Errorf("unexpected integer, got %d", n)
return
}
}
})

t.Run("Valid float64", func(t *testing.T) {
ops := []string{
`{"op":"add","path":"attr3","value":12.34}`,
`{"op":"add","path":"attr3","value":"12.34"}`,
}
for _, op := range ops {
validator, err := NewValidator(op, patchSchema)
if err != nil {
t.Errorf("unexpected error, got %v", err)
return
}
v, err := validator.Validate()
if err != nil {
t.Errorf("unexpected error, got %v", err)
return
}
n, ok := v.(float64)
if !ok {
t.Errorf("unexpected type, got %T", v)
return
}
if n != 12.34 {
t.Errorf("unexpected integer, got %f", n)
return
}
}
})

t.Run("Valid Booleans", func(t *testing.T) {
tests := []struct {
op string
expected bool
}{
{`{"op":"add","path":"attr4","value":true}`, true},
{`{"op":"add","path":"attr4","value":"True"}`, true},
{`{"op":"add","path":"attr4","value":false}`, false},
{`{"op":"add","path":"attr4","value":"False"}`, false},
}
for _, tc := range tests {
validator, err := NewValidator(tc.op, patchSchema)
if err != nil {
t.Errorf("unexpected error, got %v", err)
return
}
v, err := validator.Validate()
if err != nil {
t.Errorf("unexpected error, got %v", err)
return
}
b, ok := v.(bool)
if !ok {
t.Errorf("unexpected type, got %T", v)
return
}
if b != tc.expected {
t.Errorf("unexpected integer, got %v", b)
return
}
}
})
}

func TestOperationValidator_getRefAttribute(t *testing.T) {
Expand Down
24 changes: 12 additions & 12 deletions internal/patch/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ func Example_removeComplexMultiValuedAttributeValue() {
// <nil> <nil>
}

// The following example shows how remove a single member from a group.
func Example_removeSingleMember() {
operation := `{
"op": "remove",
"path": "members[value eq \"0001\"]"
}`
validator, _ := NewValidator(operation, schema.CoreGroupSchema())
fmt.Println(validator.Validate())
// Output:
// <nil> <nil>
}

// The following example shows how remove a single group from a user.
func Example_removeSingleGroup() {
operation := `{
Expand All @@ -59,6 +47,18 @@ func Example_removeSingleGroup() {
// [map[]] <nil>
}

// The following example shows how remove a single member from a group.
func Example_removeSingleMember() {
operation := `{
"op": "remove",
"path": "members[value eq \"0001\"]"
}`
validator, _ := NewValidator(operation, schema.CoreGroupSchema())
fmt.Println(validator.Validate())
// Output:
// <nil> <nil>
}

// The following example shows how to replace all of the members of a group with a different members list.
func Example_replaceAllMembers() {
operations := []string{`{
Expand Down
11 changes: 11 additions & 0 deletions internal/patch/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ var (
schema.SimpleCoreAttribute(schema.SimpleStringParams(schema.StringParams{
Name: "attr1",
})),
schema.SimpleCoreAttribute(schema.SimpleNumberParams(schema.NumberParams{
Name: "attr2",
Type: schema.AttributeTypeInteger(),
})),
schema.SimpleCoreAttribute(schema.SimpleNumberParams(schema.NumberParams{
Name: "attr3",
Type: schema.AttributeTypeDecimal(),
})),
schema.SimpleCoreAttribute(schema.SimpleBooleanParams(schema.BooleanParams{
Name: "attr4",
})),
schema.SimpleCoreAttribute(schema.SimpleStringParams(schema.StringParams{
Name: "multiValued",
MultiValued: true,
Expand Down
30 changes: 26 additions & 4 deletions schema/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"

datetime "github.com/di-wu/xsd-datetime"
Expand Down Expand Up @@ -179,12 +180,19 @@ func (a CoreAttribute) ValidateSingular(attribute interface{}) (interface{}, *er

return bin, nil
case attributeDataTypeBoolean:
b, ok := attribute.(bool)
if !ok {
switch b := attribute.(type) {
case bool:
return b, nil
case string:
bb, err := strconv.ParseBool(b)
if err != nil {
return nil, &errors.ScimErrorInvalidValue
}

return bb, nil
default:
return nil, &errors.ScimErrorInvalidValue
}

return b, nil
case attributeDataTypeComplex:
obj, ok := attribute.(map[string]interface{})
if !ok {
Expand Down Expand Up @@ -237,6 +245,13 @@ func (a CoreAttribute) ValidateSingular(attribute interface{}) (interface{}, *er
return f, nil
case float64:
return n, nil
case string:
f, err := strconv.ParseFloat(n, 64)
if err != nil {
return nil, &errors.ScimErrorInvalidValue
}

return f, nil
default:
return nil, &errors.ScimErrorInvalidValue
}
Expand All @@ -251,6 +266,13 @@ func (a CoreAttribute) ValidateSingular(attribute interface{}) (interface{}, *er
return i, nil
case int, int8, int16, int32, int64:
return n, nil
case string:
i, err := strconv.ParseInt(n, 10, 64)
if err != nil {
return nil, &errors.ScimErrorInvalidValue
}

return i, nil
default:
return nil, &errors.ScimErrorInvalidValue
}
Expand Down
2 changes: 1 addition & 1 deletion schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func TestValidationInvalid(t *testing.T) {
"booleans": []interface{}{
true,
},
"decimal": "1.1",
"decimal": "1,000",
},
{ // invalid type integer (json.Number)
"required": "present",
Expand Down
Loading