jquense / yup

Dead simple Object schema validation
MIT License
22.95k stars 935 forks source link

types: Lazy class is not exported #1954

Open hotgazpacho opened 1 year ago

hotgazpacho commented 1 year ago

Describe the bug The Lazy class is not exported, while the factory function to create an instance of Lazy is. All the other factory functions are paired paired with exports of the classes (NumberSchema, StringSchema, MixedSchema, etc.) that they create.

This is problematic when one uses lazy() to define a schema in one file, and export it for use in another (e.g. a React component).

The following errors are produced:

Property 'builder' of exported class expression may not be private or protected.
Exported variable 'createSchema' has or is using name 'LazySpec' from external module "<redacted>/node_modules/yup/index" but cannot be named.
Property '_resolve' of exported class expression may not be private or protected.
Property 'optionality' of exported class expression may not be private or protected.
Exported variable 'createSchema' has or is using name 'CastOptions$1' from external module "<redacted>/node_modules/yup/index" but cannot be named.
Exported variable 'createSchema' has or is using name 'CastOptionalityOptions' from external module "<redacted>/node_modules/yup/index" but cannot be named.
Exported variable 'createSchema' has or is using name 'NestedTestConfig' from external module "<redacted>/node_modules/yup/index" but cannot be named.

To Reproduce

Define a schema using lazy() and export it.

Expected behavior I am able to define a schema using lazy() and not receive any of the above TypeScript errors.

Platform (please complete the following information):

Additional context Yup 1.0.2

pointnet commented 1 year ago

I have the exact same issue with the following code:

import { lazy, AnySchema } from 'yup';

export const LazyEmptySchema = (schema: AnySchema, emptySchema: AnySchema) => {
  return lazy((value) =>
    value === undefined || value === '' ? emptySchema : schema
  );
};
ethanhoroschak commented 1 year ago

Same issue here! Without Lazy export, it breaks all my types. The below code is what I had pre v1 but lib was removed and Lazy is no longer exported.

Cannot find module 'yup/lib/Lazy' or its corresponding type declarations.

import type Lazy from 'yup/lib/Lazy';
oneyan1 commented 1 year ago

i have same issue here:

export const yupDynamicObject = (objectSchema: yup.AnyObjectSchema) => {
  return yup.lazy((value) => {
    if (!value || typeof value !== "object") {
      return objectSchema;
    }
    return objectSchema.concat(
      yup.object(Object.fromEntries(Object.keys(value).map((k) => [k, yup.mixed()])))
    );
  });

fixed from unknow type convert to AnyObjectSchema type, as:

export const yupDynamicObject = (objectSchema: yup.AnyObjectSchema) => {
  const lazySchema = yup.lazy((value) => {
    if (!value || typeof value !== "object") {
      return objectSchema;
    }
    return objectSchema.concat(
      yup.object(Object.fromEntries(Object.keys(value).map((k) => [k, yup.mixed()])))
    );
  }) as unknown;
  return lazySchema as yup.AnyObjectSchema;
};
maapteh commented 1 year ago

Is it possible to solve the issue, instead of forcing us to typecast?

eugene-kim commented 1 year ago

Currently using patch-package to deal with this until it's officially fixed. You just need to export Lazy at the bottom export of node_modules/yup/index.d.ts

jquense commented 1 year ago

PRs welcome if you want to move things along