NewValidator creates an OperationValidator based on the given JSON string and reference schemas. Returns an error if patchReq is not valid.
(patchReq string, s schema.Schema, extensions ...schema.Schema)
| 36 | // NewValidator creates an OperationValidator based on the given JSON string and reference schemas. |
| 37 | // Returns an error if patchReq is not valid. |
| 38 | func NewValidator(patchReq string, s schema.Schema, extensions ...schema.Schema) (OperationValidator, error) { |
| 39 | var operation struct { |
| 40 | Op string |
| 41 | Path string |
| 42 | Value interface{} |
| 43 | } |
| 44 | if err := json.Unmarshal([]byte(patchReq), &operation); err != nil { |
| 45 | return OperationValidator{}, err |
| 46 | } |
| 47 | |
| 48 | operation.Op = strings.ToLower(operation.Op) |
| 49 | |
| 50 | switch v := operation.Value.(type) { |
| 51 | // Okta also send the ID on PATCH requests. |
| 52 | // See: internal/idp_test/testdata/okta/update_group_name.json |
| 53 | // https://developer.okta.com/docs/reference/scim/scim-20/#update-a-specific-group-name |
| 54 | case map[string]interface{}: |
| 55 | var key string |
| 56 | var found bool |
| 57 | for k := range v { |
| 58 | if strings.ToLower(k) == "id" { |
| 59 | if found { |
| 60 | return OperationValidator{}, fmt.Errorf("duplicate attributes: %s and %s", k, key) |
| 61 | } |
| 62 | found = true |
| 63 | key = k |
| 64 | } |
| 65 | } |
| 66 | delete(v, key) |
| 67 | } |
| 68 | |
| 69 | var path *filter.Path |
| 70 | if operation.Path != "" { |
| 71 | validator, err := f.NewPathValidator(operation.Path, s, extensions...) |
| 72 | if err != nil { |
| 73 | return OperationValidator{}, err |
| 74 | } |
| 75 | if err := validator.Validate(); err != nil { |
| 76 | return OperationValidator{}, err |
| 77 | } |
| 78 | p := validator.Path() |
| 79 | path = &p |
| 80 | } |
| 81 | |
| 82 | schemas := map[string]schema.Schema{ |
| 83 | s.ID: s, |
| 84 | } |
| 85 | for _, e := range extensions { |
| 86 | schemas[e.ID] = e |
| 87 | } |
| 88 | return OperationValidator{ |
| 89 | Op: Op(operation.Op), |
| 90 | Path: path, |
| 91 | value: operation.Value, |
| 92 | |
| 93 | schema: s, |
| 94 | schemas: schemas, |
| 95 | }, nil |
searching dependent graphs…