Skip to content

index.Function.validateGraphQLRules

github-actions[bot] edited this page Sep 4, 2026 · 14 revisions

Function: validateGraphQLRules()

function validateGraphQLRules(
   schema, 
   rules, 
   options?): void;

Defined in: packages/graphql-casl/src/validateGraphQLRules.ts:124

Checks stored GraphQLRules against a schema and throws PermissionsError if any of them could not do what it says.

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) action and subject, a boolean inverted and a string reason if present;
  • action is one of Actions;
  • subject is '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 fields is a field of the subject (* patterns are let through);
  • conditions uses 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.

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.

Parameters

schema

GraphQLSchema

The schema the rules are checked against.

rules

readonly unknown[]

The stored rules, as loaded — typing is deliberately loose.

options?

ValidateGraphQLRulesOptions

See ValidateGraphQLRulesOptions.

Returns

void

Throws

Aggregating every problem, not just the first.

Example

const rules: GraphQLRule<AppSubjectMap>[] = await db.loadPermissionRules();
validateGraphQLRules(schema, rules); // throws PermissionsError on drift
const ability = buildGraphQLAbility<AppSubjectMap>(rules);

API Reference

Clone this wiki locally