$ show jquense/yup
Yup is a schema builder and validation library for JavaScript and TypeScript, designed for runtime value parsing and validation. It provides a fluent, chainable API to define schemas that can cast input values to the correct types and assert constraints. The library is built around a modular class hierarchy: a base Schema class provides common methods (validate, cast, isValid, etc.), and concrete types (StringSchema, NumberSchema, BooleanSchema, DateSchema, ArraySchema, ObjectSchema, TupleSchema) extend it with type-specific transforms and tests. MixedSchema allows arbitrary types with custom type guards. The library also supports lazy evaluation via LazySchema, conditional logic via when(), and references via Ref for cross-field validation. Yup is fully asynchronous by default but provides synchronous variants for use cases without async tests. It integrates with the Standard Schema specification for interoperability. External dependencies include property-expr for path expressions, tiny-case for string case transformations, toposort for dependency ordering, and type-fest for TypeScript utilities. The codebase is written in TypeScript with comprehensive type inference, allowing static types to be inferred from schema definitions. The architecture emphasizes immutability (each method returns a new schema instance) and composability (schemas can be concatenated, nested, and reused). Key entry points are the factory functions (string(), number(), boolean(), date(), object(), array(), tuple(), mixed(), lazy(), ref()) and the methods on schema instances (validate, cast, isValid, describe, etc.). The validation flow involves resolving conditions and references, running transforms (coercion pipeline), then executing tests (assertions). Errors are collected into ValidationError objects with detailed context.
The LLM identified 5 files worth highlighting for reuse:
A standalone ISO 8601 date parser that handles various date and datetime formats with timezone offsets. It is extracted from a well-known library (js-iso8601) and provides both parseIsoDate (returns timestamp) and parseDateStruct (returns structured components). The implementation is self-contained, well-tested, and can be reused in any JavaScript project requiring robust ISO date parsing without external dependencies.
A utility for converting arbitrary JavaScript values to human-readable strings, used in error messages. It handles special cases like NaN, Infinity, negative zero, functions, symbols, Dates, Errors, and RegExps with appropriate formatting. The function is self-contained and can be extracted for use in any project that needs to display values in error messages or logs.
A deep clone utility that handles circular references, Dates, RegExps, Maps, Sets, and plain objects. It uses a seen map to avoid infinite recursion on circular structures. The implementation is efficient and self-contained, making it suitable for reuse in any JavaScript project that requires deep cloning with support for modern data structures.
Implements a reference system for lazy resolution of values from sibling fields, parent objects, or external context. References are used in validations like oneOf, min, max, and when conditions to refer to other field values dynamically. The class is self-contained (depends only on property-expr for path parsing) and demonstrates a clean pattern for deferred value resolution that can be adapted to other validation or configuration systems.
A custom Error class for validation failures that aggregates multiple errors, supports formatted messages with parameter interpolation, and provides detailed context (path, value, type, params). It includes a static formatError method for message formatting and a static isError type guard. The class is self-contained and can be reused in any validation framework that needs structured error reporting.