programing

yup의 물건을 사용하는 방법.타자기로 모양을 만들 수 있습니까?

lastcode 2023. 6. 11. 10:41
반응형

yup의 물건을 사용하는 방법.타자기로 모양을 만들 수 있습니까?

현재 코드 일부를 TypeScript로 변환하고 있으며 몇 가지 유형 문제가 발생하고 있습니다.yup유형입니다. 여러 가지 방법을 시도해 보고 참조했습니다.yup이 문제를 해결하기 위한 TypeScript 지원에 대한 설명서가 없습니다.

https://github.com/jquense/yup/blob/master/docs/typescript.md

다음은 내 코드의 샘플입니다.

import { object, Asserts, string } from 'yup';

interface BaseBootstrapSchema {  
  APPLICATION_NAME: string;
  LOG_PATH: string;
}

const bootstrapValidationSchema = object<BaseBootstrapSchema>().shape({ // error 1 here
  LOG_PATH: string().required("The 'LOG_PATH' ENV variable is required"),
  APPLICATION_NAME: string().required("The 'APPLICATION_NAME' ENV variable is required"),
});

interface BootstrapSchema extends Asserts<typeof bootstrapValidationSchema> {}

module.exports = (schema: BootstrapSchema) =>
  new Promise((resolve, reject) =>
    schema
      .validate(process.env, { abortEarly: false }) // error 2 here
      .then(() => resolve(true))
      .catch((error: any) => {
        if (error.errors.length > 1) {
          reject(new Error(`${error.message},\n${error.errors.join(',\n')}`));
        } else {
          reject(new Error(error.message));
        }
      })
  );

다음 오류가 발생합니다.

오류 1:

'BaseBootstrapSchema' 유형이 'Record<string, AnySchema<any,any,any> | 참조 | Lazy<any,any>' 제약 조건을 충족하지 않습니다.'BaseBootstrapSchema' 유형에 인덱스 서명이 없습니다.

오류 2:

'validate' 속성이 'BootstrapSchema' 유형에 없습니다.

이 문제를 해결하기 위해 몇 가지 방법을 시도해 보았지만 실제로 효과가 있는 방법은 없습니다.

도움을 주셔서 감사합니다.

여기에 있는 yup 가이드에 따라 코드를 다음과 같이 변경해야 합니다.

import { object, SchemaOf, string } from 'yup';

interface BaseBootstrapSchema {  
  APPLICATION_NAME: string;
  LOG_PATH: string;
}

const bootstrapValidationSchema: SchemaOf<BaseBootstrapSchema> = object({
  LOG_PATH: string().required("The 'LOG_PATH' ENV variable is required"),
  APPLICATION_NAME: string().required("The 'APPLICATION_NAME' ENV variable is required"),
});

module.exports = (schema: SchemaOf<BaseBootstrapSchema>) =>
  new Promise((resolve, reject) =>
    schema
      .validate(process.env, { abortEarly: false })
      .then(() => resolve(true))
      .catch((error: any) => {
        if (error.errors.length > 1) {
          reject(new Error(`${error.message},\n${error.errors.join(',\n')}`));
        } else {
          reject(new Error(error.message));
        }
      })
  );

사실 당신은 그것이 필요하지 않습니다.const bootstrapValidationSchema당신이 내보내는 노드 모듈에 전달할 것이기 때문에, 저는 당신이 여기서 무엇을 달성하려고 하는지 오해할 수 있습니다.해보세요.

스키마의 속성이 선택적인 경우 OptionalObjectSchema 및 AnySchema 유형을 사용합니다.

import { object, string, AnySchema } from 'yup';
import { OptionalObjectSchema } from 'yup/lib/object'

interface BaseBootstrapSchema {  
  APPLICATION_NAME: string;
  LOG_PATH: string;
}

const bootstrapValidationSchema: OptionalObjectSchema<Record<keyof BaseBootstrapSchema, AnySchema>> = object({
  LOG_PATH: string(),
  APPLICATION_NAME: string(),
});

언급URL : https://stackoverflow.com/questions/66171196/how-to-use-yups-object-shape-with-typescript

반응형