Repository navigation
index.Function.validateGraphQLRules
function validateGraphQLRules(
schema,
rules,
options?): void;Defined in: packages/graphql-casl/src/validateGraphQLRules.ts:165
Checks stored GraphQLRules against a schema and throws PermissionsError if any of them could not do what it says.
GraphQLSchema
The schema the rules are checked against.
readonly unknown[]
The stored rules, as loaded — typing is deliberately loose.
See ValidateGraphQLRulesOptions.
void
PermissionsError Aggregating every problem, not just the first.
buildGraphQLAbility accepts whatever it is given, and CASL evaluates rules
lazily, so a stale rule is not an error — it is a rule that silently never
grants. That is the wrong failure mode for rows edited outside the type
system, which is what DB-backed rules are. Call this on the rows before (or
after) rehydrating them, ideally at startup and in a test.
Checked, per rule:
- the shape: an object with a string (or string array)
actionandsubject, a booleaninvertedand a stringreasonif present; -
actionis one of Actions; -
subjectis'all'or an object type in the schema — not a root operation type, an interface or a union (subjects are detected by__typename, which is always a concrete type, so a rule on an interface never matches); - each of
fieldsis a field of the subject (*patterns are let through); -
conditionsuses only operators CASL's matcher knows, does not mix operators with plain keys, and — by default — names only fields of the subject, following dotted paths through object-typed fields. See ValidateGraphQLRulesOptions.conditionFields to relax that last check when subjects are database models rather than GraphQL types, and ValidateGraphQLRulesOptions.operators to tighten the first when a leaf adapter folds the rules into a query filter.
Every problem is reported at once, labelled by the rule's index in the array. Condition values are not checked against field types; CASL compares them structurally at check time.
const rules: GraphQLRule<AppSubjectMap>[] = await db.loadPermissionRules();
validateGraphQLRules(schema, rules); // throws PermissionsError on drift
const ability = buildGraphQLAbility<AppSubjectMap>(rules);