Forest Admin Cloud
  • What is Forest Admin?
  • How to Read This Doc?
  • Quick Start
  • Core Concepts
    • Agent
    • Data Sources
    • Collections
    • Views
    • Fields
      • Types
    • Relationships
    • Actions
    • Segments
    • Dashboards
    • Workspaces
    • Layouts
    • Roles & Permissions
    • Projects
    • Organizations
  • Native Modules
    • CRUD
    • Approval Workflows
    • Activity Logs
    • Notes
    • Inbox
  • UI Customization
    • Actions
    • Workspaces
    • Analytics
      • Metabase Integration
  • Code Customization
    • Getting Started
    • Actions
      • Context
      • Response
        • Data refresh
      • Form
        • Widgets
    • Fields
      • Filtering
      • Sorting
      • Validation
    • Relationships
    • Collections
      • Search
    • Charts
  • Security Modules
    • Roles & Permissions
    • SCIM User Provisioning
    • Single Sign On
    • Two-Factor Authentication
    • Auto-logout
    • IP Whitelisting
  • Data sources settings
    • SQL
    • MongoDB
    • Multiple Data Sources
    • Database Credentials
    • Schema Synchronization
  • Best Practices
    • Performance
Powered by GitBook
On this page

Was this helpful?

  1. Code Customization
  2. Fields

Validation

This is the official documentation of Forest Admin Cloud.

Most data sources can import validation rules directly. For example, in a SQL database, columns defined as VARCHAR(15) will automatically include a rule to ensure content is shorter than 15 characters. Similarly, columns marked as NOT NULL will require that some content is always present.


To make the code easier to read, all the code snippets below should be wrapped in the following code. Ensure you update the collection and field names as needed.

import type { Agent } from '@forestadmin/forest-cloud';
import { Schema } from '../typings';

export default function customizeAgent(agent: Agent<Schema>) {
  agent.customizeCollection('users', (collection) => {
    // Insert the code snippet here.
  });
}

In some cases, it might be necessary to enforce stricter rules than the ones currently set in your data source. This can be achieved through the use of the addFieldValidation method.

collection
  .addFieldValidation('first_name', 'Present')
  .addFieldValidation('first_name', 'LongerThan', 2)
  .addFieldValidation('first_name', 'ShorterThan', 13)
  .addFieldValidation('first_name', 'Match', /^[a-z]+$/i);

Last updated 1 year ago

Was this helpful?