Docs / Builder

Validation

Validate user-provided field values against a service schema before building a URL. This gives you typed, structured errors you can display in your UI.

validateFields()

signature
function validateFields(
  service: string,
  fields: Record<string, string | number | boolean>,
): ValidationError[]

Returns an array of ValidationError objects. An empty array means all fields are valid.

ValidationError
interface ValidationError {
  field: string;    // The field key that failed
  message: string;  // Human-readable error message
}

Examples

Valid input

typescript
import { validateFields } from '@ambersecurityinc/notifly/builder';

const errors = validateFields('discord', {
  webhook_id: '1234567890',
  webhook_token: 'abcdefghijklmnopqrst',
});

console.log(errors); // []

Missing required field

typescript
const errors = validateFields('discord', {
  webhook_id: '1234567890',
  webhook_token: '',       // empty — required field
});

console.log(errors);
// [{ field: 'webhook_token', message: 'Required' }]

Pattern mismatch

typescript
const errors = validateFields('discord', {
  webhook_id: 'not-a-number',  // must match /^d+$/
  webhook_token: 'valid-token',
});

console.log(errors);
// [{ field: 'webhook_id', message: 'Invalid format' }]

Unknown service

typescript
const errors = validateFields('unknown', {});

console.log(errors);
// [{ field: 'service', message: 'Unknown service: unknown' }]

UI integration pattern

form submit handler
import { validateFields, buildUrl } from '@ambersecurityinc/notifly/builder';

function handleSubmit(formValues: Record<string, string>) {
  const errors = validateFields('discord', formValues);

  if (errors.length > 0) {
    // Map errors to form fields
    const fieldErrors: Record<string, string> = {};
    for (const err of errors) {
      fieldErrors[err.field] = err.message;
    }
    setFormErrors(fieldErrors);
    return;
  }

  const { url } = buildUrl('discord', formValues);
  saveNotificationUrl(url);
}

What gets validated

  • Required fields — empty strings or missing keys fail
  • Pattern — if validation.pattern is set, the value must match the regex
  • minLength / maxLength — string length constraints (e.g. Pushover keys must be exactly 30 chars)
  • min / max — numeric range constraints
  • select options — value must be one of the defined options[].value entries

Tip: Optional fields with empty values are not validated — only required fields trigger errors for missing values.

← Previous Schemas Next → URL Generation