admin 管理员组

文章数量: 1086019

firstName: yup
    .string()
    .test(
        'len',
        'can be empty or with string at least 2 characters and not more than 10',
        (val) => val != undefined && (val.length == 0 || (val.length >= 2 && val.length <= 10) )
    )

in this case length min 2 and max 10 works, but when is empty is marked with error i tried with val.length == 0

firstName: yup
    .string()
    .test(
        'len',
        'can be empty or with string at least 2 characters and not more than 10',
        (val) => val != undefined && (val.length == 0 || (val.length >= 2 && val.length <= 10) )
    )

in this case length min 2 and max 10 works, but when is empty is marked with error i tried with val.length == 0

Share Improve this question asked Dec 30, 2021 at 11:14 AlexAlex 9,74030 gold badges107 silver badges166 bronze badges 5
  • I guess if you want to allow the value to be empty (0) remove val.length ==0 from code. – Mohit Kushwaha Commented Dec 30, 2021 at 11:20
  • add required() after string, will fix the issue – Tigran Petrosyan Commented Dec 30, 2021 at 11:20
  • @TigranPetrosyan it mus to allow empty – Alex Commented Dec 30, 2021 at 11:22
  • 1 Could it be that val bees undefined if no text is entered? – Peter B Commented Dec 30, 2021 at 11:24
  • @PeterB yeah, that was the issue :) – Alex Commented Dec 30, 2021 at 11:27
Add a ment  | 

3 Answers 3

Reset to default 4

Hello you can do it like this

const yup = require("yup");
const firstName = "";

const schema = yup
  .string()
  .test(
    "len",
    "can be empty or with string at least 2 characters and not more than 10",
    (val) => {
      if (val === undefined) {
        return true;
      }
      return val.length === 0 || (val.length >= 2 && val.length <= 10);
    }
  );

schema
  .validate(firstName)
  .then((response) => console.log(response))
  .catch((err) => console.log(err));

fixed by separate undefined check

  firstName: yup
            .string()
            .test(
                'len',
                'can be empty or with string at least 2 characters and not more than 10',
                (val) => {
                    if (val == undefined) {
                        return true;
                    }
                    return  ((val.length == 0 || (val.length >= 2 && val.length <= 10)))
                }
            ),

Global Function,

export const minLengthValidation = (value: string | undefined, limit: number | undefined = 3) => {
  if (value && value.length < limit) {
    return false
  }

  return true
}

Invoked the function,

  nick_name: yup
    .string()
    .max(50, 'Nickname must be less than 50 characters!')
    .test('min-length', 'Nickname must be greater than  3 characters!', value => minLengthValidation(value))
    .required('Nickname is required.'),

本文标签:

Error[2]: Invalid argument supplied for foreach(), File: /www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm, Line: 58
File: /www/wwwroot/roclinux.cn/tmp/route_read.php, Line: 205, include(/www/wwwroot/roclinux.cn/tmp/view_template_quzhiwa_htm_read.htm)
File: /www/wwwroot/roclinux.cn/tmp/index.inc.php, Line: 129, include(/www/wwwroot/roclinux.cn/tmp/route_read.php)
File: /www/wwwroot/roclinux.cn/index.php, Line: 29, include(/www/wwwroot/roclinux.cn/tmp/index.inc.php)