patch

package
v0.0.0-...-f923362 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 10, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Example (AddMemberToGroup)

The following example shows how to add a member to a group.

operation := `{
	"op": "add",
	"path": "members",
	"value": {
		"display": "di-wu",
		"$ref": "https://example.com/v2/Users/0001",
		"value": "0001"
	}
}`
validator, _ := NewValidator(operation, schema.CoreGroupSchema())
fmt.Println(validator.Validate())
Output:

[map[$ref:https://example.com/v2/Users/0001 display:di-wu value:0001]] <nil>
Example (AddWithoutPath)

The following example shows how to add one or more attributes to a User resource without using a "path" attribute.

operation := `{
	"op": "add",
	"value": {
		"emails": [
			{
				"value": "quint@elimity.com",
				"type": "work"
			}
		],
		"nickname": "di-wu"
	}
}`
validator, _ := NewValidator(operation, schema.CoreUserSchema())
fmt.Println(validator.Validate())
Output:

map[emails:[map[type:work value:quint@elimity.com]] nickname:di-wu] <nil>
Example (RemoveAllMembers)

The following example shows how remove all members of a group.

operation := `{
	"op": "remove",
	"path": "members"
}`
validator, _ := NewValidator(operation, schema.CoreGroupSchema())
fmt.Println(validator.Validate())
Output:

<nil> <nil>
Example (RemoveComplexMultiValuedAttributeValue)

The following example shows how remove a value from a complex multi-valued attribute.

operation := `{
	"op": "remove",
	"path": "emails[type eq \"work\" and value ew \"elimity.com\"]"
}`
validator, _ := NewValidator(operation, schema.CoreUserSchema())
fmt.Println(validator.Validate())
Output:

<nil> <nil>
Example (RemoveSingleMember)

The following example shows how remove a single member from a group.

operation := `{
	"op": "remove",
	"path": "members[value eq \"0001\"]"
}`
validator, _ := NewValidator(operation, schema.CoreGroupSchema())
fmt.Println(validator.Validate())
Output:

<nil> <nil>
Example (ReplaceAllMembers)

The following example shows how to replace all of the members of a group with a different members list.

operations := []string{`{
	"op": "remove",
	"path": "members"
}`, `{
	"op": "add",
	"path": "members",
	"value": [
		{
			"display": "di-wu",
			"$ref": "https://example.com/v2/Users/0001",
			"value": "0001"
		},
		{
			"display": "example",
			"$ref": "https://example.com/v2/Users/0002",
			"value": "0002"
		}
	]
}`,
}
for _, op := range operations {
	validator, _ := NewValidator(op, schema.CoreGroupSchema())
	fmt.Println(validator.Validate())
}
Output:

<nil> <nil>
[map[$ref:https://example.com/v2/Users/0001 display:di-wu value:0001] map[$ref:https://example.com/v2/Users/0002 display:example value:0002]] <nil>
Example (ReplaceAnyAttribute)

The following example shows how to replace all values of one or more specific attributes.

operation := `{
	"op": "replace",
	"value": {
		"emails": [
			{
				"value": "quint",
				"type": "work",
				"primary": true
			},
			{
				"value": "me@di-wu.be",
				"type": "home"
			}
		],
		"nickname": "di-wu"
	}
}`
validator, _ := NewValidator(operation, schema.CoreUserSchema())
fmt.Println(validator.Validate())
Output:

map[emails:[map[primary:true type:work value:quint] map[type:home value:me@di-wu.be]] nickname:di-wu] <nil>
Example (ReplaceMembers)

The following example shows how to replace all of the members of a group with a different members list in a single replace operation.

operations := []string{`{
	"op": "replace",
	"path": "members",
	"value": [
		{
			"display": "di-wu",
			"$ref": "https://example.com/v2/Users/0001",
			"value": "0001"
		},
		{
			"display": "example",
			"$ref": "https://example.com/v2/Users/0002",
			"value": "0002"
		}
	]
}`,
}
for _, op := range operations {
	validator, _ := NewValidator(op, schema.CoreGroupSchema())
	fmt.Println(validator.Validate())
}
Output:

[map[$ref:https://example.com/v2/Users/0001 display:di-wu value:0001] map[$ref:https://example.com/v2/Users/0002 display:example value:0002]] <nil>
Example (ReplaceSpecificSubAttribute)

The following example shows how to change a specific sub-attribute "streetAddress" of complex attribute "emails" selected by a "valuePath" filter.

operation := `{
	"op": "replace",
	"path": "addresses[type eq \"work\"].streetAddress",
	"value": "ExampleStreet 100"
}`
validator, _ := NewValidator(operation, schema.CoreUserSchema())
fmt.Println(validator.Validate())
Output:

ExampleStreet 100 <nil>
Example (ReplaceWorkAddress)

The following example shows how to change a User's entire "work" address, using a "valuePath" filter.

operation := `{
	"op": "replace",
	"path": "addresses[type eq \"work\"]",
	"value": {
		"type": "work",
		"streetAddress": "ExampleStreet 1",
		"locality": "ExampleCity",
		"postalCode": "0001",
		"country": "BE",
		"primary": true
	}
}`
validator, _ := NewValidator(operation, schema.CoreUserSchema())
fmt.Println(validator.Validate())
Output:

[map[country:BE locality:ExampleCity postalCode:0001 streetAddress:ExampleStreet 1 type:work]] <nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Op

type Op string

Op represents the possible value the operation is to perform. Possible values are one of "add", "remove", or "replace".

const (
	// OperationAdd is used to add a new attribute value to an existing resource.
	OperationAdd Op = "add"
	// OperationRemove removes the value at the target location specified by the required attribute "path".
	OperationRemove Op = "remove"
	// OperationReplace replaces the value at the target location specified by the "path".
	OperationReplace Op = "replace"
)

type OperationValidator

type OperationValidator struct {
	Op   Op
	Path *filter.Path
	// contains filtered or unexported fields
}

OperationValidator represents a validator to validate PATCH requests.

func NewValidator

func NewValidator(patchReq string, s schema.Schema, extensions ...schema.Schema) (OperationValidator, error)

NewValidator creates an OperationValidator based on the given JSON string and reference schemas. Returns an error if patchReq is not valid.

func (OperationValidator) Validate

func (v OperationValidator) Validate() (interface{}, error)

Validate validates the PATCH operation. Unknown attributes in complex values are ignored. The returned interface contains a (sanitised) version of given value based on the attribute it targets. Multi-valued attributes will always be returned wrapped in a slice, even if it is just one value that was defined within the operation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL