inline-schema/csv-loader.md

89 lines
2.0 KiB
Markdown
Raw Normal View History

2026-03-31 13:02:29 +08:00
# inline-schema/csv-loader
A rspack loader for CSV files that uses inline-schema for type validation.
## Installation
```bash
npm install inline-schema
```
## Usage
The loader expects:
- **First row**: Property names (headers)
- **Second row**: Inline-schema definitions for each property
- **Remaining rows**: Data values
### Example CSV
```csv
name,age,active,scores
string,number,boolean,number[]
Alice,30,true,[90; 85; 95]
Bob,25,false,[75; 80; 70]
```
### rspack.config.js
```javascript
module.exports = {
module: {
rules: [
{
test: /\.schema\.csv$/,
use: {
loader: 'inline-schema/csv-loader',
options: {
delimiter: ',',
quote: '"',
escape: '\\',
2026-03-31 14:45:02 +08:00
bom: true, // 处理 BOM (默认 true)
comment: '#', // 忽略 # 开头的注释行 (默认 '#')
trim: true, // 修剪表头和值的前后空格 (默认 true)
2026-03-31 13:02:29 +08:00
},
},
},
],
},
};
```
### Importing in TypeScript
```typescript
import data from './data.schema.csv';
// data = [
// { name: "Alice", age: 30, active: true, scores: [90, 85, 95] },
// { name: "Bob", age: 25, active: false, scores: [75, 80, 70] }
// ]
```
## Options
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `delimiter` | string | `,` | Column delimiter |
| `quote` | string | `"` | Quote character |
| `escape` | string | `\` | Escape character |
2026-03-31 14:45:02 +08:00
| `bom` | boolean | `true` | Handle byte order mark |
| `comment` | string \| `false` | `#` | Comment character (set `false` to disable) |
| `trim` | boolean | `true` | Trim headers and values |
2026-03-31 13:02:29 +08:00
## Schema Syntax
Uses [inline-schema](https://github.com/your-repo/inline-schema) syntax:
| Type | Schema | Example |
|------|--------|---------|
| String | `string` | `hello` |
| Number | `number` | `42` |
| Boolean | `boolean` | `true` |
| Array | `string[]` or `[string][]` | `[a; b; c]` |
| Tuple | `[string; number]` | `[hello; 42]` |
## License
ISC