Predicate Spec
A _predicate/spec
is set of smart functions that are attached to a particular predicate. Any time that predicate appears in a transaction, regardless of who issues the transaction, the smart functions attached to the _predicate/spec
will execute.
If all of the smart functions return true
, the transaction will succeed. If any of the smart functions return false
, it will fail.
When you add a function to a predicate, it cannot take any parameters.
Predicate Spec Using Universal Functions
If you add a function using only universal functions, (unless using functions like random or now) that predicate will always return the same result. For example:
[
{
"_id": "_fn$alwaysTrue",
"name": "alwaysTrue",
"code": "(== 13 (add3 10))",
"doc": "I always return true"
},
{
"_id": ["_predicate/name", "person/handle"],
"spec": ["_fn$alwaysTrue"],
"specDoc": "I always return true"
}
]
Adding the above function to the _predicate/spec
for person/handle
is relatively useless, because the function will always return true
, therefore this smart function would never stop any type of transaction.
Predicate Spec Using Context-Dependent Functions
When adding a function that includes context-dependent functions your smart function will be able to control who makes an update or the value in that update.
For example, this function make sure person/favNums
is non-negative, but using ?o
, which returns the object of our predicate.
[
{
"_id": "_fn$nonNegative?",
"name": "nonNegative?",
"doc": "Checks that a value is non-negative",
"code": "(<= 0 (?o))"
},
{
"_id": ["_predicate/name", "person/favNums"],
"spec": ["_fn$nonNegative?"]
}
]