首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AdonisJS存在Validator

AdonisJS存在Validator
EN

Stack Overflow用户
提问于 2021-08-10 18:28:07
回答 1查看 468关注 0票数 0

我正在跟踪官方的文档 && 指示性,但是我找不到任何帮助我的东西。

如果给定的param存在于数据库上,我希望验证

所以我试着:

app/验证器/myValidator

代码语言:javascript
复制
const { rule } = use('Validator')

get rules () {
    return {
      userId: 'required|integer|exists:MyDatabase.users,userId', <-- this is what isn't working
      date: [
        rule('date'),
        rule('dateFormat', 'YYYY-MM-DD')
      ]
    }
  }

// Getting the data to be validated
  get data () {
    const params = this.ctx.params

    const { userId, date } = params

    return Object.assign({}, { userId }, { date })
  }

它给出了以下错误:

代码语言:javascript
复制
{
  "error": {
    "message": "select * from `MyDatabase`.`users`.`userId` where `undefined` = '2' limit 1 - ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.`userId` where `undefined` = '2' limit 1' at line 1",
    "name": "ErrorValidator",
    "status": 40
  }
}

如何正确地指示要将MyDatabase.users.userid与给定参数进行比较?

EN

回答 1

Stack Overflow用户

发布于 2021-08-10 19:23:44

经过几次艰苦的尝试/错误之后,我偶然发现了这个解决方案。

只需遵循hooks.js内部的内容,并传递以逗号分隔的值,如下所示:

代码语言:javascript
复制
get rules () {
  return {
    userId: 'required|integer|exists:MyDatabase,MyTable,columntoCompare',      
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68731892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档