validateArgs

View as MarkdownOpen in Claude

Validate arguments against the parameter JSON Schema. Uses Ajv to compile and run the schema. When the function has no parameters declared (empty schema), validation is skipped and the arguments are treated as valid — matching the Python SDK’s early-return behavior.

Parameters

args
Record<string, unknown>Required

Arguments to validate.

Returns

[boolean, string[]] — A tuple of [isValid, errors]. When no validation is needed (empty schema), returns [true, []]. Error strings include the offending property path where available (e.g., "'account_id' is a required property").

Example

1import { SwaigFunction, FunctionResult } from '@signalwire/sdk';
2
3const fn = new SwaigFunction({
4 name: 'lookup_account',
5 handler: async () => new FunctionResult('ok'),
6 description: 'Look up account status',
7 parameters: {
8 type: 'object',
9 properties: {
10 account_id: { type: 'string', description: 'Account ID' },
11 },
12 required: ['account_id'],
13 },
14});
15
16// Valid arguments
17const [ok1, errs1] = fn.validateArgs({ account_id: '12345' });
18console.log(`Valid: ${ok1}, Errors: ${JSON.stringify(errs1)}`);
19// Valid: true, Errors: []
20
21// Missing required argument
22const [ok2, errs2] = fn.validateArgs({});
23console.log(`Valid: ${ok2}, Errors: ${JSON.stringify(errs2)}`);
24// Valid: false, Errors: ["must have required property 'account_id'"]