# csv/v1 — CSV parse, validate, stats, JSON conversion

Parses and generates CSV, using Go's standard library `encoding/csv`. Its
dialect: comma-default (configurable to any single character), double-quote
field quoting, and both `\n` and `\r\n` line endings — not a general
configurable separator/quote/escape combination. A leading UTF-8
byte-order mark is stripped automatically; no other encoding is detected
or converted.

No field ever has its type guessed — every value is a string, both in and
out. A field like `"01"` stays `"01"`, never becomes the number `1`.

### `POST /v1/csv/parse`

**Request**

```json
{ "text": "id,name\n01,Ala\n", "delimiter": "," }
```

- `text` (required).
- `delimiter` (optional, default `,`) — a single character.

**Response**

```json
{ "rows": [["id", "name"], ["01", "Ala"]] }
```

Ragged rows (inconsistent field counts) are tolerated — short rows are
returned as-is, not padded. Use `/validate` to detect them.

### `POST /v1/csv/validate`

Same request shape as `/parse`. Reports whether every row has the same
field count as the first, and whether the document is otherwise
structurally well-formed (e.g. no unterminated quotes).

**Response (valid)**

```json
{ "valid": true, "rowCount": 2, "fieldCount": 2 }
```

**Response (invalid)**

```json
{ "valid": false, "error": "parse error: record on line 2: wrong number of fields" }
```

### `POST /v1/csv/stats`

Same request shape as `/parse`; the first row is always treated as a
header. Reports per-column completeness — non-empty vs. empty value counts
across the data rows. No numeric aggregation (min/max/mean) is computed,
since that requires guessing which columns are numeric.

**Response**

```json
{
  "rowCount": 2,
  "columnCount": 2,
  "columns": [
    { "name": "id", "nonEmpty": 2, "empty": 0 },
    { "name": "name", "nonEmpty": 1, "empty": 1 }
  ]
}
```

### `POST /v1/csv/to_json`

**Request**

```json
{ "text": "id,name\n01,Ala\n", "header": true }
```

- `header` (optional, default `true`) — if `true`, returns one object per
  data row keyed by the header row; if `false`, returns raw rows (same
  shape as `/parse`).

**Response**

```json
{ "result": [{ "id": "01", "name": "Ala" }] }
```

### `POST /v1/csv/from_json`

The inverse of `/to_json`'s header mode. `columns` is the explicit,
ordered header — JSON object key order isn't preserved by Go's JSON
decoder, so the column order can't be inferred from `records` alone.

**Request**

```json
{ "columns": ["id", "name"], "records": [{ "id": "01", "name": "Ala" }] }
```

**Response**

```json
{ "text": "id,name\n01,Ala\n" }
```

A key in `records` that isn't listed in `columns` is silently dropped; a
column in `columns` missing from a record becomes an empty field.

**Error response**

```json
{ "error": "delimiter must be a single character, got \"::\"" }
```

## Notes

- `/from_json`'s output is not sanitized against spreadsheet formula
  injection (fields starting with `=`, `+`, `-`, or `@`, which some
  spreadsheet apps execute as formulas on open) — sanitize before opening
  untrusted output in Excel/Sheets if that's a concern.

## Out of scope

Type-guessing on parse, numeric statistics, alternate quote/escape
characters, and non-UTF-8 encodings — ask if you need one added.

## Files

- `csv.go` — module wiring (routes, HTTP handlers)
- `convert.go` — pure parse/validate/stats/conversion logic
- `convert_test.go` — known-vector and error tests
