Skip to content

Fields ​

Select which resource fields are returned, or extend/shrink the server's default selection.

URL keyfields
AST nodesFields / Field { name, operator?, condition? }
Schema optionsallowed, default, mapping, validate / validateMany

On the wire ​

txt
fields=id,name,email          one list for the root resource
fields[items]=id,name         per-relation lists
fields[$root]=id,name         root list when combined with per-relation lists
fields=+email                 extend the default selection
fields=-name                  shrink it

A lone root list keeps the bare fields= form. As soon as root and relation fieldsets travel together, the URL codec spells the root group with the reserved $root token (fields[$root]=id,name&fields[items]=id); a bare fields= cannot be combined with fields[...]= groups in the same URL.

The equivalent parser input shapes (what SimpleParser and the URL decoder accept):

typescript
{ fields: 'id,name,email' }                    // comma-separated string
{ fields: ['id', 'name'] }                     // array of names
{ fields: { items: ['id', 'name'] } }          // record keyed by relation
{ fields: [['id'], { items: ['id'] }] }        // tuple: base + relations

Include & exclude modifiers ​

A field name can carry a prefix that changes how it combines with the schema's default selection:

SyntaxMeaning
nameSelect this field (replaces the default selection).
+nameInclude: extends the default selection instead of replacing it.
-nameExclude: removes the field from the selection.
typescript
// schema default is ['id', 'name']
{ fields: '+email' }   // → id, name, email
{ fields: '-name' }    // → id

In the AST, the prefix becomes Field.operator (FieldOperator.INCLUDE / FieldOperator.EXCLUDE).

Building in code ​

The same shapes work as typed build input; field paths are checked against the record type:

typescript
defineQuery<User>({ fields: ['id', '+email'] });

defineFields<User>(['id', 'name']);   // standalone fragment

Fields of a relation use the relation name as key (or a relation.field path) and validate against the related schema, resolved through schemaMapping. The relation must be permitted: listed in relations.allowed when the schema declares it (without that list it is permitted, unless the schema is strict), and accepted by the relations hooks, which judge every relation a fields key traverses. When the request carries an include parameter, even a blank one, or the schema's default includes stand in for an absent one, the relation must also be among the includes; a fieldset of a relation the request does not include is dropped. Without an include parameter and without default includes, a relation field auto-joins its relation, as a dotted filter or sort key does.

typescript
{
    relations: ['items'],
    fields: { items: ['id', 'name'] },
}

A per-relation fieldset governs the projection of an included relation: the backends hydrate items with exactly the listed columns (@rapiq/adapter-typeorm additionally selects the relation's primary key, so the relation object hydrates reliably even when every listed column is NULL). When the related schema declares fields.default (or an allowed list), those apply to the include the same way. Only an include without any fieldset, against a schema without a fields block, hydrates the whole record.

Schema options ​

typescript
defineSchema<User>({
    fields: {
        allowed: ['id', 'name', 'email', 'age'],
        default: ['id', 'name'],
        mapping: { displayName: 'name' },
    },
});
OptionDescription
allowedSelectable field names. Omit to allow all; [] blocks the parameter.
defaultSelection when the client sends nothing (or only +/- modifiers).
mappingAlias → field translation applied before validation.
validate / validateManyPer-request key hooks: accept, reject, or gate a field per actor.

The hooks judge every field the response will contain: the names a client sends, and the names the schema adds itself (a request without a fields parameter expands default, or without one allowed; +/- modifiers start from the same set). A gate such as validateMany: (keys, actor) => keys.filter((key) => key !== 'secret' || actor.canReadSecret) therefore also holds for a plain GET /users. A name the schema added that a hook rejects is dropped silently, even under throwOnFailure, since the client never asked for it; a condition verdict gates it like a requested field. Excluded (-name) fields are never judged.

An empty fieldset reads as every column to every backend, and an include without a fieldset hydrates the whole record. So when the hooks deny every field of the root or of an included relation, the parse fails closed with KEY_VALIDATE_REJECTED for the denied names, whatever throwOnFailure says, rather than widening the projection to the names it denied. The same holds when the client input empties the root or an included relation by itself (fields=id,-id, or a - on every name the level would project): the level falls back to its input-less projection (default, or the allowed expansion) minus the excluded names, which the hooks judge like any name the schema adds, and the parse fails closed with KEY_VALIDATE_REJECTED when the excludes leave nothing. A relation the query does not include projects nothing once the client excluded all of its names (fields[items]=-id without include=items), so that request succeeds unchanged. A schema without default and allowed has no projection to fall back to, so excluding its names still yields the empty (every column) fieldset. Keep at least one field the actor may read in default (or allowed). The keys of a key-value relation are exempt: an emptied key-value fieldset implies no include, and next to one the whole map is read through the deferred key gate, which keeps the denied keys out.

Row-scoped fields ​

A validate / validateMany hook may answer with a condition instead of a boolean. The field stays selected, but the condition lands on the Field node as Field.condition and marks the column visible only on rows satisfying it:

typescript
import { eq } from '@rapiq/core';

defineSchema<User, Actor>({
    name: 'user',
    fields: {
        allowed: ['id', 'name', 'salary'],
        validate: (field, actor) => field !== 'salary' ||
            eq('realm_id', actor.realmId),
    },
});

The gate never removes a row; it only blanks a column on the rows that fail it. @rapiq/adapter-memory applies it while projecting; the SQL backends project the column and rely on a post-fetch pass. Details and the fail-open caveat: Condition verdicts.

A gate also resists composition: a merge collision that would replace a gated field with an ungated one (or with a different gate) throws a MergeError (ErrorCode.FIELDS_CONDITION_DISCARDED) instead of silently widening disclosure.

Key-value table fields ​

Names stored as (name, value) rows of a side table can be served as ordinary root keys, selected through a follow-up load and filtered with eq, ne, in, nin and null checks. See Key-value table fields (TypeORM) and Key-value tables (plain SQL).

Key-value relations ​

fields[attributes]=department,app.theme selects named keys of a key-value relation and implies its include; the keys reach the parent schema's fields.validate / validateMany hooks as attributes.<key>.

On violation ​

Disallowed or invalid field input is dropped silently; with throwOnFailure it fails the parse instead (FieldsParseError from parseFields, the general ParseError from a whole-query parse, each carrying the rejection on its issue trace).

Released under the MIT License.