inline-schema/csv-loader.md

1.6 KiB

inline-schema/csv-loader

A rspack loader for CSV files that uses inline-schema for type validation.

Installation

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

name,age,active,scores
string,number,boolean,number[]
Alice,30,true,[90; 85; 95]
Bob,25,false,[75; 80; 70]

rspack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.schema\.csv$/,
        use: {
          loader: 'inline-schema/csv-loader',
          options: {
            delimiter: ',',
            quote: '"',
            escape: '\\',
          },
        },
      },
    ],
  },
};

Importing in 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

Schema Syntax

Uses 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