Skip to main content

Data Validation

Ductape lets you define validation rules for Action inputs, ensuring data consistency and integrity before requests are sent.

Overview

When you import Actions, Ductape automatically detects input parameters from the request body, query strings, headers, and route parameters. You can then configure validation rules to:

  • Enforce required fields
  • Set minimum and maximum lengths
  • Define data types
  • Provide default values
  • Add decorators for display formatting

Prerequisites

Before configuring validation, ensure you have:

  1. The Ductape SDK installed and initialized
  2. An App with imported Actions
import Ductape from '@ductape/sdk';

const ductape = new Ductape({
workspace_id: 'your-workspace-id',
user_id: 'your-user-id',
private_key: 'your-public-key',
});

Validation Properties

FieldTypeDescription
descriptionstringExplanation of the field's purpose
requiredbooleanWhether the field is mandatory
maxLengthnumberMaximum allowed length for strings
minLengthnumberMinimum allowed length for strings
decoratorstringText or symbol displayed alongside the value
decoratorPositionDecoratorPositionsPosition of the decorator (PREPEND, APPEND, or UNSET)
typeDataTypesThe expected data type
defaultValuestring | number | booleanDefault value if none is provided
sampleValuestring | number | objectExample demonstrating expected format

Selector Format

To update validation, you need to specify which field you're targeting using a selector string:

SelectorTarget
$Body{action_tag}{...}{key}Request body fields
$Query{action_tag}{...}{key}URL query parameters
$Params{action_tag}{...}{key}Route parameters
$Header{action_tag}{...}{key}HTTP headers

Nested Fields

For nested objects, chain the keys:

$Body{createUser}{user}{profile}{firstName}

This targets body.user.profile.firstName in the createUser Action.

Updating Validation Rules

Use ductape.apps.validation() to set validation rules:

await ductape.apps.validation(selector, validationRules);

Example: Username Validation

await ductape.apps.validation('$Body{createUser}{user}{username}', {
description: 'Username for the new account',
required: true,
type: 'nospaces_string',
maxLength: 30,
minLength: 3,
defaultValue: '',
});

Example: Price Field with Decorator

import { DecoratorPositions } from '@ductape/sdk/types';

await ductape.apps.validation('$Body{createProduct}{product}{price}', {
description: 'Price of the product in USD',
required: true,
type: 'float',
decorator: '$',
decoratorPosition: DecoratorPositions.PREPEND,
defaultValue: 0.0,
});

Example: Array of Tags

await ductape.apps.validation('$Body{createBlogPost}{post}{tags}', {
description: 'Tags associated with the blog post',
type: 'array-string',
defaultValue: [],
});

Example: Required UUID Parameter

await ductape.apps.validation('$Params{getUser}{id}', {
type: 'uuid',
required: true,
description: 'Unique identifier for the user',
});

Data Types

TypeValueDescriptionExample
STRING"string"General string"Hello World"
NOSPACES_STRING"nospaces_string"String without spaces"NoSpaces"
EMAIL_STRING"email_string"Valid email format"user@example.com"
NUMBER_STRING"numberstring"Number in string format"12345"
INTEGER"number"Whole number42
FLOAT"float"Floating-point number3.14
DOUBLE"double"Double-precision number123.456789
UUID"uuid"UUID format"550e8400-e29b-41d4..."
ARRAY"array"General array[1, "text", true]
OBJECT"object"Object structure{ key: "value" }
BOOLEAN"boolean"Boolean valuetrue
STRING_ARRAY"array-string"Array of strings["apple", "banana"]
INTEGER_ARRAY"array-number"Array of integers[1, 2, 3]
FLOAT_ARRAY"array-float"Array of floats[1.1, 2.2, 3.3]
DOUBLE_ARRAY"array-double"Array of doubles[1.123456, 2.654321]
UUID_ARRAY"array-uuid"Array of UUIDs["550e8400-..."]
BOOLEAN_ARRAY"array-boolean"Array of booleans[true, false]

Decorator Positions

PositionValueDescriptionExample
PREPEND"BEFORE"Decorator appears before value$100
APPEND"AFTER"Decorator appears after value100$
UNSET""No decorator100

Complete Example

Here's a complete example setting up validation for a user registration Action:

import Ductape from '@ductape/sdk';
import { DecoratorPositions } from '@ductape/sdk/types';

async function setupValidation() {
const ductape = new Ductape({
workspace_id: 'your-workspace-id',
user_id: 'your-user-id',
private_key: 'your-public-key',
});

// Username validation
await ductape.apps.validation('$Body{registerUser}{username}', {
description: 'Unique username for the account',
required: true,
type: 'nospaces_string',
minLength: 3,
maxLength: 20,
});

// Email validation
await ductape.apps.validation('$Body{registerUser}{email}', {
description: 'Email address for account verification',
required: true,
type: 'email_string',
});

// Age validation
await ductape.apps.validation('$Body{registerUser}{age}', {
description: 'User age (must be 18 or older)',
required: false,
type: 'number',
defaultValue: 18,
});

// Roles validation
await ductape.apps.validation('$Body{registerUser}{roles}', {
description: 'User roles for access control',
required: false,
type: 'array-string',
defaultValue: ['user'],
});

console.log('Validation rules configured successfully');
}

setupValidation().catch(console.error);

Best Practices

Use Meaningful Descriptions

// Good
await ductape.apps.validation('$Body{createOrder}{total}', {
description: 'Total order amount including tax and shipping',
});

// Avoid
await ductape.apps.validation('$Body{createOrder}{total}', {
description: 'Total', // Too vague
});

Set Appropriate Constraints

// Prevent data overflow
await ductape.apps.validation('$Body{createPost}{title}', {
maxLength: 200,
minLength: 1,
required: true,
});

Use Default Values for Optional Fields

await ductape.apps.validation('$Body{createUser}{status}', {
required: false,
defaultValue: 'pending',
});

Choose the Correct Data Type

// Use specific types for better validation
await ductape.apps.validation('$Body{sendEmail}{to}', {
type: 'email_string', // Not just 'string'
required: true,
});

Next Steps

See Also