FeaturesValidation
Features

Validation

Data validation is essential to ensure that imported data meets your requirements. ExpressCSV provides robust validation capabilities to help ensure data quality.

Built-in Validators

ExpressCSV comes with several built-in validators:

  • Required Fields - Ensure required fields have values
  • Email Validation - Validate email formats
  • Number Validation - Ensure values are numbers within specified ranges
  • Date Validation - Validate date formats and ranges
  • Pattern Matching - Validate values against regular expressions

Custom Validation

You can define custom validation functions for more complex requirements:

const columns = [
  { 
    name: 'email', 
    label: 'Email Address', 
    required: true,
    validate: (value) => {
      if (!value.includes('@')) {
        return 'Invalid email format';
      }
      return true; // Valid
    }
  },
  { 
    name: 'age', 
    label: 'Age', 
    validate: (value) => {
      const age = parseInt(value, 10);
      if (isNaN(age)) {
        return 'Age must be a number';
      }
      if (age < 18 || age > 100) {
        return 'Age must be between 18 and 100';
      }
      return true; // Valid
    }
  }
];

Row-Level Validation

In addition to validating individual fields, you can also validate entire rows:

const importer = new ExpressCSV({
  element: '#csv-importer',
  columns: [...],
  validateRow: (row) => {
    // Check if either phone or email is provided
    if (!row.phone && !row.email) {
      return {
        valid: false,
        errors: ['Either phone or email must be provided']
      };
    }
    
    // Check if start date is before end date
    if (row.startDate && row.endDate) {
      const start = new Date(row.startDate);
      const end = new Date(row.endDate);
      if (start > end) {
        return {
          valid: false,
          errors: ['Start date must be before end date']
        };
      }
    }
    
    return { valid: true };
  }
});

Error Handling and Reporting

ExpressCSV provides comprehensive error handling:

  • Inline Error Messages - Show errors next to the problematic fields
  • Error Summary - Show a summary of all errors
  • Error Highlighting - Highlight rows and cells with errors
  • Error Export - Export errors for offline resolution

Example

Here's a complete example of setting up validation:

const importer = new ExpressCSV({
  element: '#csv-importer',
  columns: [
    { 
      name: 'email', 
      label: 'Email Address', 
      required: true,
      validate: (value) => /\S+@\S+\.\S+/.test(value) || 'Invalid email format'
    },
    { 
      name: 'phone', 
      label: 'Phone Number',
      validate: (value) => {
        if (!value) return true; // Optional field
        return /^\d{10}$/.test(value.replace(/\D/g, '')) || 'Invalid phone format';
      }
    },
    {
      name: 'zipCode',
      label: 'ZIP Code',
      validate: (value) => {
        if (!value) return true; // Optional field
        return /^\d{5}(-\d{4})?$/.test(value) || 'Invalid ZIP code';
      }
    }
  ],
  validateRow: (row) => {
    if (!row.email && !row.phone) {
      return {
        valid: false,
        errors: ['At least one contact method (email or phone) is required']
      };
    }
    return { valid: true };
  }
});

Next Steps

After validating the data, you can explore using the API to customize the import experience further.