Skip to main content
Last updated

Keysets

define-keyset

Use define-keyset to define a keyset as name with keyset, or if unspecified, read name from the message payload as a keyset, similarly to read-keyset. If the keyset name already exists, the keyset will be enforced before updating to the new value.

Basic syntax

To define a keyset as name with keyset, or read name from the message payload, use the following syntax:

(define-keyset name keyset) (define-keyset name)

Arguments

Use the following arguments to specify the inputs for the define-keyset Pact function:

ArgumentTypeDescription
namestringSpecifies the name of the keyset to define or read.
keysetstringSpecifies the keyset to associate with the name.

Return values

The define-keyset function returns a string representing the result of defining the keyset.

Examples

The following examples demonstrate the define-keyset function:

  1. Define a keyset named 'admin-keyset' with a specified keyset:
pact
(define-keyset 'admin-keyset "my-keyset")
pact
(define-keyset 'admin-keyset "my-keyset")
  1. Read the keyset from the message payload and associate it with 'admin-keyset':
pact
(define-keyset 'admin-keyset)
pact
(define-keyset 'admin-keyset)

enforce-keyset

Use enforce-keyset to execute a specified GUARD or a defined keyset named KEYSETNAME to enforce the desired predicate logic.

Basic syntax

To execute a GUARD or a defined keyset to enforce desired predicate logic, use the following syntax:

(enforce-keyset GUARD) (enforce-keyset KEYSETNAME)

Arguments

Use the following arguments to specify the GUARD or KEYSETNAME for the enforce-keyset Pact function:

ArgumentTypeDescription
GUARDguardSpecifies the guard to execute.
KEYSETNAMEstringSpecifies the name of the defined keyset to enforce.

Return values

The enforce-keyset function returns a boolean value indicating whether the guard or keyset enforced the desired predicate logic.

Examples

The following examples demonstrate the enforce-keyset function:

  1. Execute a guard named 'admin-keyset' to enforce desired logic:
pact
(enforce-keyset 'admin-keyset)
pact
(enforce-keyset 'admin-keyset)
  1. Execute a row guard named 'row-guard' to enforce desired logic:
pact
(enforce-keyset row-guard)
pact
(enforce-keyset row-guard)

In these examples, the enforce-keyset function is used to execute the specified guard or keyset to enforce the desired predicate logic. The function returns a boolean value indicating whether the guard or keyset enforced the desired logic successfully.

keys-2

Use keys-2 as a keyset predicate function to determine if at least two keys are matched in the keyset.

Basic syntax

To use keys-2 to check if at least two keys are matched in a keyset, use the following syntax:

keys-2 count matched

Arguments

Use the following arguments to specify the count of keys in the keyset and the count of matched keys using the keys-2 Pact function.

ArgumentTypeDescription
countintegerSpecifies the total count of keys in the keyset.
matchedintegerSpecifies the count of matched keys.

Return value

The keys-2 function returns a boolean value indicating whether at least two keys are matched in the keyset.

Examples

The following example demonstrates the use of keys-2 in the Pact REPL:

pact
pact>(keys-2 3 1)false
pact
pact>(keys-2 3 1)false

In this example, keys-2 checks if at least two keys are matched in a keyset where the total count of keys is 3 and only 1 key is matched. The function returns false, indicating that the condition of having at least two keys matched is not met.

keys-all

Use keys-all as a keyset predicate function to determine if all keys in the keyset are matched.

Basic syntax

To use keys-all to check if all keys in a keyset are matched, use the following syntax:

(keys-all count matched)

Arguments

Use the following arguments to specify the count of keys in the keyset and the count of matched keys using the keys-all Pact function.

ArgumentTypeDescription
countintegerSpecifies the total count of keys in the keyset.
matchedintegerSpecifies the count of matched keys.

Return value

The keys-all function returns a boolean value indicating whether all keys in the keyset are matched.

Examples

The following example demonstrates the use of keys-all in the Pact REPL:

pact
pact>(keys-all 3 3)true
pact
pact>(keys-all 3 3)true

In this example, keys-all checks if all keys are matched in a keyset where the total count of keys is 3 and all 3 keys are matched. The function returns true, indicating that all keys in the keyset are matched.

keys

Use keys to return all keys present in a specified table.

Basic syntax

To retrieve all keys present in a table, use the following syntax:

(keys table)

Arguments

Use the following argument to specify the table from which you want to retrieve keys using the keys Pact function.

ArgumentTypeDescription
tabletable<{row}>Specifies the table from which keys will be retrieved.

Return value

The keys function returns a list of strings containing all keys present in the specified table.

Examples

The following example demonstrates the use of keys in the Pact REPL to retrieve all keys present in the "accounts" table:

pact
(keys accounts)
pact
(keys accounts)

In this example, all keys present in the "accounts" table are returned as a list of strings.