npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

typed-csv

v2.0.0

Published

A TypeScript library for typed CSV data with inline schema validation

Readme

typed-csv

A TypeScript library for typed CSV data with inline schema validation using a TypeScript-like syntax with ; instead of ,.

Installation

npm install typed-csv

Usage

Basic Example

import { defineSchema } from 'typed-csv';

// Define a schema
const stringSchema = defineSchema('string');
const numberSchema = defineSchema('number');
const booleanSchema = defineSchema('boolean');

// Parse values
const name = stringSchema.parse('hello');        // "hello"
const age = numberSchema.parse('42');            // 42
const active = booleanSchema.parse('true');      // true

// Validate parsed values
stringSchema.validator(name);    // true
numberSchema.validator(name);    // false

Tuples

const tupleSchema = defineSchema('[string; number; boolean]');

const value1 = tupleSchema.parse('[hello; 42; true]');
// ["hello", 42, true]

tupleSchema.validator(value1);    // true
tupleSchema.validator(['a', 'b', true]);  // false (second element should be number)

Arrays

// Array syntax: Type[]
const stringArray = defineSchema('string[]');
const numberArray = defineSchema('number[]');

const names = stringArray.parse('[alice; bob; charlie]');
// ["alice", "bob", "charlie"]

const numbers = numberArray.parse('[1; 2; 3; 4; 5]');
// [1, 2, 3, 4, 5]

Array of Tuples

const schema = defineSchema('[string; number][]');

const data = schema.parse('[[a; 1]; [b; 2]; [c; 3]]');
// [["a", 1], ["b", 2], ["c", 3]]

Escaping Special Characters

Use \ to escape special characters ;, [, ], and \ in string values:

const schema = defineSchema('string');

const value1 = schema.parse('hello\\;world');    // "hello;world"
const value2 = schema.parse('hello\\[world');    // "hello[world"
const value3 = schema.parse('hello\\\\world');   // "hello\\world"

// In tuples
const tupleSchema = defineSchema('[string; string]');
const tuple = tupleSchema.parse('hello\\;world; test');
// ["hello;world", "test"]

String Identifiers

Any identifier (including hyphens) is treated as a string schema:

const schema = defineSchema('word-smith');
const value = schema.parse('word-smith');
// "word-smith"

API

defineSchema(schemaString: string): ParsedSchema

Parses a schema string and returns an object with:

  • schema: The parsed schema AST
  • validator: A function to validate values against the schema
  • parse: A function to parse value strings

parseSchema(schemaString: string): Schema

Parses a schema string and returns the schema AST.

parseValue(schema: Schema, valueString: string): unknown

Parses a value string according to the given schema.

createValidator(schema: Schema): (value: unknown) => boolean

Creates a validation function for the given schema.

schemaToTypeString(schema: Schema): string

Converts a schema AST back to a human-readable type string.

ParseError

Error class thrown for invalid schema syntax.

Types

The following TypeScript types are exported:

  • Schema — union of all schema AST node types
  • ParsedSchema — the return type of defineSchema()
  • PrimitiveSchema, TupleSchema, ArraySchema — AST node types
  • ReferenceSchema, ReverseReferenceSchema — reference AST node types
  • StringLiteralSchema, UnionSchema — literal and union AST node types

Schema Syntax

| Type | Schema | Example Value | |------|--------|---------------| | String | string or identifier | hello | | Int | int | 42 | | Float | float | 3.14 | | Number | number | 42 or 3.14 |

Note: int, float, and number all collapse to the number type in generated TypeScript declarations (schemaToTypeString). The distinction is preserved at parse time — int rejects non-integer values while float/number accept them. | Boolean | boolean | true or false | | Tuple | [Type1; Type2; ...] | [hello; 42; true] | | Array | Type[] | [1; 2; 3] | | Array of Tuples | [Type1; Type2][] | [[a; 1]; [b; 2]] | | Union | Type1 \| Type2 | hello or 42 (reference members tried first) | | String Literal | 'on' \| 'off' or "red" | on or off | | Reference | @tablename or @tablename[] | (resolved at CSV load time) | | Reverse Reference | ~tablename(fk) | (resolved at CSV load time) |

Notes

  • Semicolons ; are used as separators instead of commas ,
  • Tuple and array values must be wrapped in brackets [] (e.g. [a; b])
  • [single] is a 1-tuple, not an array — use Type[] for arrays
  • In a union, reference members are tried before non-reference members (e.g. @users | string resolves 1 to the user object, falling back to a plain string when the reference doesn't match)
  • Special characters can be escaped with backslash: \;, \[, \], \\
  • Empty arrays/tuples are not allowed
  • In CSV schema rows, double-quoted string literals like "active" | "inactive" are handled automatically by the loader; avoid commas inside string literals (use single-quoted literals like 'a,b' for those)
  • For CSV loading with reference resolution, see csv-loader.md

Migration from 1.x

Version 2.0.0 introduces breaking changes to the schema DSL:

  1. Composite values must be fully bracketed. Tuple and array values now always require [][a; 1]; [b; 2] is no longer accepted; write [[a; 1]; [b; 2]].
  2. The [Type][] array form is removed. Use Type[] (e.g. [string; number][] instead of [[string; number]][]).
  3. [single] is now a 1-tuple, not an array. Use Type[] for single-element arrays.
  4. Union resolution prefers reference members. In a union containing references, reference members are tried before non-reference members regardless of author order (see the notes above).
  5. Schema cells may be fully quoted. Double-quoted string literals like "active" | "inactive" in a CSV schema row are handled automatically by the loader.
// rspack.config.js
module.exports = {
  module: {
    rules: [
      {
        test: /\.schema\.csv$/,
        use: 'typed-csv/csv-loader',
      },
    ],
  },
};

License

ISC