【问题标题】:YupSchema. how validate only first element of array in YupSchema是的架构。如何仅验证 YupSchema 中数组的第一个元素
【发布时间】:2023-03-04 18:20:01
【问题描述】:

我有这个架构:

const YupSchema = (t: TFunction) =>
Yup.object().shape({
    name: Yup.string()
        .required('*')
        .max(49, 'maxСharacters' + ' 50'),
    areas: Yup.array().required('*'),        
    activities: Yup.array().of(
        Yup.object().shape({
            id: Yup.string().required('*'),
            pictureGood: Yup.string().required(
                'Required'
            ),
            pictureBad: Yup.string().required(
                'Required'
            ),
            translations: Yup.array() /* <=== hear I need get only first element in array*/
                .of(
                    Yup.object().shape({
                        name: Yup.string().required('*'),
                        desc: Yup.string()
                            .required('*')
                            .max(999, 'maxСharacters' + ' 1000'),
                       
                        locale: Yup.string()
                            .required('*')
                            .max(999, 'maxСharacters' + ' 1000')
                    })
                )
                .required('Translations Required')
        })
    )
})

对于这个数据对象:

[{"id":"","activitiId":"1","pictureGood":[],"pictures":[],"translations":[{"generalDesc":"test","descGood":"test","descBad":"test","locale":"IT"},"generalDesc":"test","descGood":"test","descBad":"test","locale":"EN"}]}]

但我只需要验证数组中的第一个元素,而不是全部。像这样的东西: .

...

translations: Yup.array()[0]
            .of(
                Yup.object().shape({
                    name: Yup.string().required('*'),

...

感谢您的回答!

【问题讨论】:

    标签: javascript arrays validation yup


    【解决方案1】:

    我也想做同样的事情,一旦我按照以下方式扩展了“from”对象,我就可以看到“index”属性出现了: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/49512 ...但我无法进入索引。

    所以使用该技术(我将在下面复制以获得完整答案)我设法还扩展了来自 TextContext 的“选项”对象,然后扩展了“ValidateOptions”对象(我可以通过逐步查看我可以看到索引值的浏览器!) - 因此我可以检查我在数组中的测试所在的索引(因为它循环遍历数组中的所有项目)。

    我在数组上运行这个 yup.test(),然后简单地剔除任何不是 index=0 的东西(例如,react/javascript 中的 index === 0!)。

    所以我的解决方案是:

    interface ValidateOptionsExtended {
        options: {
            index: number;
        };
    }
    
    Yup.array().of(
        Yup.object().shape({
             outletId: yup
                .string()
                .trim()
                .nullable()
                .test('firstOutletAlwaysMandatory', 'Organisation name is required', function (item) {
                            const { from } = this as yup.TestContext & TestContextExtended;
                            const { options } = this as yup.TestContext & ValidateOptionsExtended;
                            // if not index 0 then exit we are not checking others here
                            if (options.index > 0) {
                               return true; 
                            }
    
                            ...check required value etc for index 0 here...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 1970-01-01
      相关资源
      最近更新 更多