validate
Validates data against validation rules. Supports two modes: rich validation with rule files (recommended) and legacy single-rule mode (backward compatible).
Rich Validation with Rule Files
Use YAML/JSON rule files for comprehensive, reusable validation:
# Validate with rule file
undatum validate data.csv --rules validation-rules.yml
undatum validate workbook.xlsx --table Sheet2 --rules validation-rules.yml
undatum validate nested.jsonl --rules rules.yml --flatten-nested
undatum validate data.csv --rules rules.yml --filter '`status` == "active"'
# Filter by severity
undatum validate data.jsonl --rules rules.yml --severity error
# JSON output for CI/CD integration
undatum validate data.csv --rules rules.yml --format-out json
# Generate detailed violation report
undatum validate data.jsonl --rules rules.yml --violation-report violations.json
# Treat warnings as errors
undatum validate data.csv --rules rules.yml --fail-on-warnings
Rule File Format:
Rule files support field-level and cross-field validation with severity levels. Built-in
formats (country, currency, language, date, phone, iban, uuid, pattern, ...),
unique, and references to another file are listed on Validation rules
and by undatum validate --list-rules. -O jsonl prints one violation per line.
rules:
# Field-level rules
- field: email
name: Email Required
description: Email field must be present
required: true
type: string
format: email
severity: error
- field: age
name: Age Range
description: Age must be between 0 and 120
type: number
min: 0
max: 120
severity: warning
- field: status
name: Status Values
type: string
enum: [active, inactive, pending]
severity: error
# Cross-field validation
- type: cross-field
name: Date Range Validation
description: End date must be after start date
condition: "end_date >= start_date"
fields: [start_date, end_date]
severity: error
Rule Types:
- Required:
required: true- Field must be present and non-empty - Type:
type: string|number|integer|float|boolean- Value type validation - Format:
format: email|url|uuid- Format validation - Range:
min,maxfor numbers;min_length,max_lengthfor strings - Enum:
enum: [value1, value2, ...]- Whitelist validation - Pattern:
pattern: 'regex'- Regular expression validation - Custom:
custom: 'rule_name'- Use custom validation function from VALIDATION_RULEMAP - Cross-field:
type: cross-fieldwithconditionexpression
Severity Levels:
error: Hard errors that should block processingwarning: Soft warnings that don't block processinginfo: Informational violations
Violation Reporting:
The validation command provides comprehensive reporting:
- Summary statistics: Total violations by severity, by field, by rule
- Detailed violations: Record-level violation details with context
- JSON output: Machine-readable format for CI/CD integration
- Violation report file: Detailed JSON report with all violations
Example Rule Files:
Example rule files are available in examples/validation-rules/:
basic-validation.yml- Common field-level validation rulescross-field-validation.yml- Cross-field validation examplescomplex-validation.yml- Comprehensive validation scenario
Engines:
- For CSV and TSV files,
--engine auto(the default) checks a rule file as one DuckDB query, about 4× faster on large files (1M rows: 1.4 s instead of 5.8 s). - It reports the same violations, in the same order and with the same messages, as the Python
engine, which
--engine pythonselects. - It applies to field rules made of
required,type,references,unique,min/max,min_length/max_length,enumandpattern. - The file is checked record by record in Python when:
- a rule uses
formatorcustom, or is a cross-field rule; - a pattern uses
\w,\s,\bor a construct DuckDB's regular expressions lack (Python reads them differently); - a value is a number only Python reads (digits or spaces outside ASCII);
- the
--filterhas no SQL form; - the input is not CSV or TSV.
- a rule uses
undatum quality --rulesuses the same query.
Data Contracts (ODCS)
--rules also takes an Open Data Contract Standard
(ODCS v3) contract, a YAML file with kind: DataContract. undatum schema --format odcs writes a
draft contract for a file:
undatum schema data.csv --format odcs --output contract.odcs.yaml
undatum validate data.csv --rules contract.odcs.yaml
undatum validate data.csv --rules contract.odcs.yaml --format-out json
A contract with several schema objects needs the name of one after #:
--rules contract.odcs.yaml#orders.
apiVersion: v3.2.0
kind: DataContract
id: orders-contract
version: 1.0.0
schema:
- name: orders
properties:
- name: order_id
logicalType: integer
primaryKey: true
- name: email
logicalType: string
logicalTypeOptions: {format: email}
quality:
- metric: nullValues
mustBeLessThan: 5
unit: percent
severity: warning
- name: status
enum: [{value: open}, {value: shipped}]
quality:
- metric: rowCount
mustBeGreaterThan: 0
How the parts of a contract are checked:
| Contract | Checked as |
|---|---|
required, unique, primaryKey (one property) | Rules, record by record |
logicalType: integer, number | The integer and number rule types |
logicalType: boolean, date, timestamp | The boolean, date and datetime formats |
enum | Allowed values (numbers also as their text, as CSV holds them) |
logicalTypeOptions: minLength, maxLength, pattern, minimum, maximum, format (email, uuid, url) | Rules; a pattern matches anywhere in the value, as in ODCS |
Quality metrics nullValues, missingValues, invalidValues, duplicateValues, rowCount | Contract checks: counted over the file and compared with mustBe, mustBeLessThan, mustBeBetween, ... in rows or percent |
primaryKey on several properties, exclusiveMinimum, exclusiveMaximum, multipleOf | Contract checks |
sql, custom and text checks, other types and options | Listed as not checked |
Rules report violations per record, as from a rule file, on both engines. Contract checks are
listed in their own table, and in the contract key of the JSON report. A failed check of
severity error (the default) makes the command exit with 1, and one of severity warning
does so with --fail-on-warnings. nullValues counts missing fields, nulls and empty values,
because CSV has no other null. SQL checks are never run: a contract from elsewhere could run any
SQL. undatum quality --rules and Dataset.validate() check the rules of a contract, not its contract checks.
Legacy Mode (Backward Compatible)
Simple single-rule validation for quick checks:
# Validate email addresses
undatum validate --rule common.email --fields email data.jsonl
# Validate Russian INN
undatum validate --rule ru.org.inn --fields VendorINN data.jsonl --mode stats
# Output invalid records
undatum validate --rule ru.org.inn --fields VendorINN data.jsonl --mode invalid
Available built-in validation rules:
common.email- Email address validationcommon.url- URL validationru.org.inn- Russian organization INN identifierru.org.ogrn- Russian organization OGRN identifierinteger- Integer validation
Validation Best Practices
- Use errors for critical issues: Fields that must be correct for data processing
- Use warnings for data quality: Issues that should be reviewed but don't block processing
- Organize rules by domain: Group related rules in separate files (e.g.,
user-validation.yml,order-validation.yml) - Version control rule files: Track rule changes and share across teams
- Use cross-field rules sparingly: They're more complex and slower to evaluate
- Test rules incrementally: Start with basic rules, add complexity as needed
Reference
Reads: any readable format · Writes: validation report or the (in)valid rows · Memory: streaming · Engines: auto, duckdb, python
undatum validate [OPTIONS] [INPUT_FILE]
| Argument | Description |
|---|---|
INPUT_FILE | Path to input file (not needed with --list-rules). |
| Option | Description | Default |
|---|---|---|
-o, --output TEXT | Optional output file path. If not specified, prints to stdout. | |
-f, --fields TEXT | Comma-separated list of field names to validate (legacy mode). | |
-d, --delimiter TEXT | CSV delimiter character (auto-detected when omitted). | |
--quotechar TEXT | CSV quote character (iterabledata default '"' when omitted). | |
--encoding TEXT | Input encoding (e.g., 'utf8', 'cp1251'); detected when omitted. | |
--verbose / --no-verbose | Enable verbose logging output. | --no-verbose |
-F, --format-in TEXT | Override input file format detection (e.g., 'csv', 'jsonl'). | |
--zipfile / --no-zipfile | Treat input file as a ZIP archive. | --no-zipfile |
--rule TEXT | Validation rule name (legacy mode, e.g., 'common.email', 'common.url'). | |
--filter, --filter-expr TEXT | Filter expression to apply before validation. | |
--mode TEXT | Legacy --rule output: 'invalid' (default), 'valid', 'all', or 'stats' (JSON counts). | invalid |
--rules TEXT | Path to YAML/JSON rule file for rich validation. | |
--severity TEXT | Filter violations by severity: 'error', 'warning', 'info', or 'all' (default). | all |
-O, --format-out TEXT | Output format: 'text' (default) or 'json'. | text |
--violation-report TEXT | Path to write detailed violation report (JSON format). | |
--fail-on-warnings / --no-fail-on-warnings | Treat warnings as errors (exit with non-zero code). | --no-fail-on-warnings |
--max-violations INTEGER | Maximum number of violations to display (default: 10 for text, 100 for JSON). | |
--progress / --no-progress | Show progress bar. | --no-progress |
--threads INTEGER | Worker processes for rule-file validation chunk parallelism. Omit for sequential processing. | |
--table, --sheet TEXT | Table or sheet name for multi-table sources (Excel, SQLite, lakehouse). | |
--start-page INTEGER | Sheet index (0-based) for Excel files. | 0 |
--trust | Acknowledge pickle deserialization risk when reading pickle sources. | |
--on-error TEXT | Parse-error policy: raise (default), skip, or warn. | |
--error-log TEXT | Append parse errors as JSONL (use with --on-error skip or warn). | |
--flatten-nested | Unfold nested dict / array-of-dict fields into dotted paths (e.g. city.lat). | |
--max-nested-depth INTEGER | With --flatten-nested, maximum nest depth to unfold (engine default 5). | |
--keep-nested-parents / --no-keep-nested-parents | With --flatten-nested, keep parent dict/array fields alongside dotted children. | --keep-nested-parents |
--json | Print the result as one JSON document (same as --format-out json). | |
--list-rules | List the built-in rules (format: ...) and exit. | |
-e, --engine [auto|duckdb|python] | Processing engine: auto (default), duckdb, or python. |
Deprecated spellings (removed in 3.0): --output-format → --format-out.
See also shared options.