Arbitrary.schema: no generation override for a struct's cross-field filter, and lookahead patterns are skipped quietly

オープン
#10,479 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@effect-bot がすでに取り組んでいます。

2026年11月9日 から。

評価

この issue はまだ評価されていません。

説明

Version: effect 4.1.1 (effect/Arbitrary; @effect/vitest 4.1.1 it.prop calls Arbitrary.schema on Schema inputs, so it fails the same way).

Two gaps in Arbitrary.schema. Both end in SampleError after 2001 discards.

1. A cross-field filter on a struct has no generation override
import {Arbitrary, Effect, Schema} from "effect";

const Dead = Schema.Struct({
	tries: Schema.Int.check(Schema.isGreaterThanOrEqualTo(1)),
	errors: Schema.Array(Schema.String).check(Schema.isMaxLength(32)),
}).check(Schema.makeFilter((v) => v.errors.length === v.tries || "one error per try"));

await Effect.runPromise(Arbitrary.sampleEffect(Arbitrary.schema(Dead), {count: 200, seed: 119120}));
// SampleError {generated: 29, discards: 2001}

arbitraryConstraint cannot say "these two fields agree", and toCodecArbitrary is read only on declarations. In v3, an arbitrary annotation on any schema could replace generation. Could a filter, or any node, take a generation override (an Arbitrary, or a toCodecArbitrary-style link)? Today the workaround is to keep a hand-built Arbitrary next to the schema and pass that to it.prop, so the schema and its generator can drift apart.

2. isPattern with a lookahead is skipped quietly
const TraceId = Schema.String.check(Schema.isPattern(/^(?!0{32})[0-9a-f]{32}$/));
await Effect.runPromise(Arbitrary.sampleEffect(Arbitrary.schema(TraceId), {count: 200, seed: 119120}));
// SampleError {generated: 0, discards: 2001}

The pattern is not compiled for generation, so the generator falls back to random strings and the filter discards all of them. Two asks, either helps: support lookahead (or at least a leading negative lookahead), or throw at derivation with an error that names the pattern it could not compile, instead of the discard error at sample time.

Workaround in use: the same set without a lookahead, isPattern(/^[0-9a-f]{32}$/) plus a filter t !== "0".repeat(32), generates 500 of 500 valid values.

主要言語
TypeScript
スター
16.7k
フォーク
808
平均マージ
17時間 9分
マージ済み PR(30日)
825

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Effect-TS/effect のほかの issue

Effect-TS/effect の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。