fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
5.96k stars 185 forks source link

feat: Add TransformStream Validation #777

Open BlackAsLight opened 1 month ago

BlackAsLight commented 1 month ago

A TransformStream that validates each chunk of a stream asserting each chunk is of a provided schema.

I've found something like this would work.

import { BaseIssue, BaseSchema, InferOutput, parse } from "@valibot/valibot"

export class VStream<TSchema extends BaseSchema<unknown, unknown, BaseIssue<unknown>>>
    extends TransformStream<unknown, InferOutput<TSchema>> {
    constructor(schema: TSchema) {
        super({
            transform(chunk, controller) {
                controller.enqueue(parse(schema, chunk))
            },
        })
    }
}

Then it could be used like this.

const readable = ReadableStream.from(async function* () {
    for (let i = 0; i < 10; ++i)
        yield new Array(Math.ceil(Math.random() * 5)).fill(0).map(_ => Math.floor(Math.random() * 10))
}())
    .pipeThrough(new VStream(v.array(v.number())))

for await (const array of readable)
    console.log(array)
fabian-hiller commented 1 month ago

This is out of scope for now, but maybe something we will look at again in the future.