toErrorSchema 方法问题

来源:9-4 转换错误信息到errorSchema

慕数据8456579

2021-04-10

老师, 我在校验过程中遇到个问题就是,当我在schema里加上required属性的时候,错误是挂在不上去的,我跟踪了一下发现,因为required好像只有在default里不存在key的时候才能触发,这个时候property其实是空的,我稍微改变了一下就能够显示错误了,你能看看吗,我也不太确定是不是我哪操作有误

之前

function toErrorSchema(errors: TransformedErrorObject[]) {
  if (errors.length < 1) return {}

  return errors.reduce((errorSchema, error) => {
    const { property, message } = error
    const path = toPath(property) // .pass1 /obj/a -> [obj, a]
    let parent = errorSchema

之后

function toErrorSchema(errors: FormatErrorsObject[]) {
  if (errors.length < 1) {
    return {};
  }

  return errors.reduce((errorSchema, error) => {
    let getProperty = "";
    const { name, property, message } = error;
    if (property === "" && name === "required") {
      const newMsg = message?.split('').slice(8).join('');
      getProperty = '.' + newMsg;
    } else {
      getProperty = property;
    }
    const path = toPath(getProperty); // /id/'123 -> [id, 123]
    let parent = errorSchema;
写回答

1回答

Jokcy

2021-04-14

jaonschema的校验就如你所说required就是只校验key存不存在的,如果你要校验空字符串,可以通过minLength

0
0

Vue3.0+TS打造企业级组件库 前端中高级开发者必修课

深度匹配高级前端工程师用人需求 打造属于你自己的开源项目

1310 学习 · 250 问题

查看课程