fastify-xrcs
validate.card
Validate a card object against RCS constraints without throwing — returns { valid, errors }.
validate.card
Validates a plain card object against RCS constraints (title length, button count, button title length). Returns a result object with valid and errors — never throws.
Signature
fastify.xrcs.validate.card(card: object): { valid: boolean; errors: string[] }
Params
| Name | Type | Required | Description |
|---|---|---|---|
card | object | Yes | A plain card object (typically from CardBuilder.build()) |
Returns
{ valid: boolean; errors: string[] }
errors is an empty array when valid: true.
Checks performed:
- Title ≤ 200 characters
- Max 2 buttons
- Each button title ≤ 25 characters
Throws
Does not throw.
Examples
Validate before sending
fastify.post("/send-card", async (request, reply) => {
const { phone, card } = request.body;
const result = fastify.xrcs.validate.card(card);
if (!result.valid) {
return reply.code(422).send({ errors: result.errors });
}
return fastify.xrcs.sendCard(`+1${phone}`, card);
});
Batch validate a set of cards
const cards = buildCampaignCards(data);
const invalid = cards
.map((card, i) => ({ i, ...fastify.xrcs.validate.card(card) }))
.filter((r) => !r.valid);
if (invalid.length > 0) {
throw new Error(`${invalid.length} invalid cards: ${JSON.stringify(invalid)}`);
}
See Also
- validate.carousel(carousel) — validate a carousel object
- card() / CardBuilder — build cards with built-in validation on
.build()
AI Context
package: "@xenterprises/fastify-xrcs"
method: fastify.xrcs.validate.card(card)
use-when: Validate a card object without throwing — returns { valid, errors } for pre-flight checks
returns: { valid: boolean, errors: string[] }
no-credentials: available in builder-only mode
