# CSV validator API

> Validate CSV against a Frictionless Table Schema or column rules: types, required, unique, ranges, regex, enums, Vietnamese phone / CCCD / tax code.

- Endpoint: `POST https://apibox.io.vn/v1/csv-validate`
- Category: Data & JSON
- HTML docs: https://apibox.io.vn/docs/csv-validate/ (Vietnamese: https://apibox.io.vn/vi/docs/csv-validate/)
- Online tool: https://apibox.io.vn/tools/csv-validate/
- Authentication: `Authorization: Bearer <API key>` or `X-API-Key: <API key>` (free key: https://apibox.io.vn/dashboard/#signup)

Send a CSV (`csv` text, raw body or file; the delimiter and old Vietnamese encodings are detected) and either `rules` — `{"email": "required|email", "luong": "integer|min:0", "sdt": "vn_phone", "phong": "enum:KT,HR"}` — or a Frictionless `schema` (`fields` with `type`, `format`, `constraints` required / unique / minimum / maximum / minLength / maxLength / pattern / enum, plus `primaryKey`, `uniqueKeys` and `missingValues`).

Types: string, integer, number (also 1.234.567,5 style), boolean, date (`format` such as `%d/%m/%Y`, or `any`), datetime, time, year, yearmonth, duration, geopoint, object, array; string formats email, uri, uuid and Vietnamese checks: `vn_phone` (mobile / landline, +84 forms), `vn_cccd` (12-digit citizen ID with province code and birth year), `vn_tax_code` (10 or 13 digits with the check digit), `vn_bank_account`, `vn_plate`.

The report gives `valid`, error counts by code, per-column summaries (errors, missing values, min / max) and up to `max_errors` (1,000) errors with row number (as in a spreadsheet, header = row 1), column, field, value and message. Structure errors — missing or extra columns, duplicate headers, rows with too few / many cells, blank rows — are always checked. Up to 500,000 rows; nothing is stored.

## Parameters

| Name | In | Type | Required | Default | Description |
|---|---|---|---|---|---|
| `csv` | body | string | yes |  | CSV text, or a file (multipart `file`, `csv_base64`) or a text/csv body. |
| `rules` | body | object | no |  | Column rules: `{"col": "required\|integer\|min:0"}` or objects. |
| `schema` | body | object | no |  | Frictionless Table Schema (instead of rules). |
| `delimiter` | body | string | no |  | Delimiter (auto-detected): `,` `;` `tab` `\|`. |
| `header` | body | boolean | no | `true` | The first row holds the column names. |
| `encoding` | body | string | no | `auto` | File encoding (auto: UTF-8, Windows-1258, TCVN3, VNI). |
| `allow_extra_columns` | body | boolean | no | `false` | With a schema: accept columns not in it. |
| `max_errors` | body | number | no | `1000` | Errors listed (1-10,000); counts are always complete. |

## Example request

```bash
curl -X POST "https://apibox.io.vn/v1/csv-validate" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"csv":"ma_nv,ho_ten,sdt,mst,luong\nNV001,Nguyễn Văn An,0912345678,0100109106,15000000\nNV002,,0123456,0100109107,-5\n","rules":{"ma_nv":"required|unique","ho_ten":"required","sdt":"vn_phone","mst":"vn_tax_code","luong":"integer|min:0"}}'
```

## Example response

```json
{
  "valid": false,
  "rows": 2,
  "columns": 5,
  "headers": [
    "ma_nv",
    "ho_ten",
    "sdt",
    "mst",
    "luong"
  ],
  "delimiter": ",",
  "encoding": null,
  "error_count": 4,
  "errors_by_code": {
    "required": 1,
    "type_error": 2,
    "minimum": 1
  },
  "fields": [
    {
      "name": "ma_nv",
      "type": "string",
      "column": 1,
      "errors": 0,
      "missing": 0
    },
    {
      "name": "ho_ten",
      "type": "string",
      "column": 2,
      "errors": 1,
      "missing": 1
    },
    {
      "name": "sdt",
      "type": "string",
      "format": "vn_phone",
      "column": 3,
      "errors": 1,
      "missing": 0
    },
    {
      "name": "mst",
      "type": "string",
      "format": "vn_tax_code",
      "column": 4,
      "errors": 1,
      "missing": 0
    },
    {
      "name": "luong",
      "type": "integer",
      "column": 5,
      "errors": 1,
      "missing": 0,
      "min": -5,
      "max": 15000000
    }
  ],
  "errors": [
    {
      "row": 3,
      "column": 2,
      "field": "ho_ten",
      "value": "",
      "code": "required",
      "message": "Value is required"
    },
    {
      "row": 3,
      "column": 3,
      "field": "sdt",
      "value": "0123456",
      "code": "type_error",
      "message": "\"0123456\" is not a Vietnamese phone number"
    },
    {
      "row": 3,
      "column": 4,
      "field": "mst",
      "value": "0100109107",
      "code": "type_error",
      "message": "\"0100109107\" is not a valid tax code (check digit does not match)"
    },
    {
      "row": 3,
      "column": 5,
      "field": "luong",
      "value": "-5",
      "code": "minimum",
      "message": "-5 is below the minimum 0"
    }
  ],
  "truncated": false
}
```

## Use cases

- Check employee or customer lists before importing them
- Validate supplier price lists and stock files in a pipeline
- Give users a cell-by-cell error report for bulk uploads
- Enforce a shared Frictionless schema across teams

## FAQ

### Rules or schema?

rules is a compact syntax for quick checks; schema follows the Frictionless Table Schema standard and adds primary / unique keys, missing values and number options (decimalChar, groupChar).

### Which row numbers are reported?

Spreadsheet numbering: the header is row 1, the first data row is row 2.

### Can it check Excel files?

Export the sheet as CSV first, or convert it with /v1/xlsx-convert?format=csv.
