- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是 User
的代码具有 JOI (14.3) 验证的模型。该代码曾经在创建新用户时有效,但最近似乎感冒并抛出 xxx not allowed
的错误:
require('dotenv').config({path: process.cwd() +'\\config\\.env'});
const jwt = require('jsonwebtoken');
const moment = require('moment');
const Joi = require('joi');
const Sql = require('sequelize');
const db = require("../startup/db");
const User = db.define('user', {
name: {type: Sql.STRING,
allowNull: false,
min: 2,
max: 50,
},
email: {type: Sql.STRING,
isEmail: true
},
cell: {type: Sql.STRING,
allowNull: false,
min: 10,
max: 20,
},
cell_country_code: {type: Sql.STRING,
allowNull: false
},
comp_name: {type: Sql.STRING
},
status: {type: Sql.STRING,
allowNull: false,
isIn: ['active', 'blocked', 'inactive', 'pending', 'unverified']
},
role: {type: Sql.STRING,
allowNull: false
},
device_id: {type: Sql.STRING, //maybe empty when the user is initially created.
},
user_data: {type: Sql.JSONB
},
last_updated_by_id: {type: Sql.INTEGER},
fort_token: {type: Sql.STRING,
allowNull: false,
min: 20 //64 for production
},
createdAt: Sql.DATE,
updatedAt: Sql.DATE
}, {
indexes: [
{
//For same fort_token, name to be unique
unique: true,
fields: ['name', 'fort_token']
}, {
//unique cell
//unique: true,
fields: ['cell_country_code', 'cell', 'status']
}, {
fields: ['cell_country_code', 'cell']
}, {
//email
fields: ['email']
}, {
fields: ['device_id']
}, {
fields: ['status']
}, {
fields: ['fort_token']
}
]
});
function validateUser(user) {
const schema = {
name: Joi.string()
.min(2)
.max(50)
.required()
.trim(),
cell: Joi.string()
.min(10)
.max(20)
.trim()
.required()
.error(new Error('该手机号有误!')),
cell_country_code: Joi.string()
.trim()
.required(),
role: Joi.string()
.required()
.trim(),
email: Joi.string()
.email()
.allow("")
.optional()
};
return Joi.validate(user, schema);
};
new user data : { _device_id: '8c9c25711c7d0262',
cell: '8008006414 ',
cell_country_code: '1',
name: 'ss9',
corp_name: '',
role: 'eventer',
email: '',
user_data: { avatar: '' } }
error in user validate : { ValidationError: "dataValues" is not allowed. "_previousDataValues" is not allowed. "_changed" is not allowed. "_modelOptions" is not allowed. "_options" is not allowed. "isNewRecord" is not allowed
at Object.exports.process (C:\d\code\js\emps_bbone\node_modules\joi\lib\errors.js:203:19)
at internals.Object._validateWithOptions (C:\d\code\js\emps_bbone\node_modules\joi\lib\types\any\index.js:764:31)
at module.exports.internals.Any.root.validate (C:\d\code\js\emps_bbone\node_modules\joi\lib\index.js:147:23)
at validateUser (C:\d\code\js\emps_bbone\models\user.js:106:16)
at router.post (C:\d\code\js\emps_bbone\routes\users.js:217:27)
at newFn (C:\d\code\js\emps_bbone\node_modules\express-async-errors\index.js:16:20)
at Layer.handle [as handle_request] (C:\d\code\js\emps_bbone\node_modules\express\lib\router\layer.js:95:5)
at next (C:\d\code\js\emps_bbone\node_modules\express\lib\router\route.js:137:13)
at C:\d\code\js\emps_bbone\middleware\auth_role.js:7:7
at newFn (C:\d\code\js\emps_bbone\node_modules\express-async-errors\index.js:16:20)
at Layer.handle [as handle_request] (C:\d\code\js\emps_bbone\node_modules\express\lib\router\layer.js:95:5)
at next (C:\d\code\js\emps_bbone\node_modules\express\lib\router\route.js:137:13)
at module.exports (C:\d\code\js\emps_bbone\middleware\auth_userinfo.js:106:13)
isJoi: true,
name: 'ValidationError',
details:
[ { message: '"dataValues" is not allowed',
path: [Array],
type: 'object.allowUnknown',
context: [Object] },
{ message: '"_previousDataValues" is not allowed',
path: [Array],
type: 'object.allowUnknown',
context: [Object] },
{ message: '"_changed" is not allowed',
path: [Array],
type: 'object.allowUnknown',
context: [Object] },
{ message: '"_modelOptions" is not allowed',
path: [Array],
type: 'object.allowUnknown',
context: [Object] },
{ message: '"_options" is not allowed',
path: [Array],
type: 'object.allowUnknown',
context: [Object] },
{ message: '"isNewRecord" is not allowed',
path: [Array],
type: 'object.allowUnknown',
context: [Object] } ],
_object:
user {
dataValues:
{ id: null,
name: 'ss9',
cell: '8008006414',
cell_country_code: '1',
email: '',
role: 'eventer' },
_previousDataValues:
{ name: undefined,
cell: '8008006414 ',
cell_country_code: undefined,
email: undefined,
role: undefined },
_changed:
{ name: true,
cell: true,
cell_country_code: true,
email: true,
role: true },
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: false,
underscored: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: [Object],
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: {},
indexes: [Array],
name: [Object],
omitNull: false,
sequelize: [Sequelize],
hooks: {} },
_options: { isNewRecord: true, _schema: null, _schemaDelimiter: '' },
isNewRecord: true },
annotate: [Function] }
error in new user
try {
user = new User(_.pick(req.body, ["name", "cell", "cell_country_code", "email", "role" ]));
const { error } = validateUser(user); //<<====== throws error with JOI.validate()
console.log("error in user validate : ", error);
if (error) {console.log("error in new user "); return res.status(400).send(error.details[0].message)};
最佳答案
您需要传递带有“allowUnknown:true”的选项作为验证函数的第三个参数
https://github.com/hapijs/joi/blob/v15.0.3/API.md#validatevalue-schema-options-callback
关于node.js - JOI.validate() 不允许 "dataValues",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56468951/
当我尝试输入时,我正在关注 Ray Wenderlich ( https://videos.raywenderlich.com/screencasts/545-server-side-swift-wi
我正在使用 javax.validation.Validation 来验证 jpa 实体。我总是针对相同的实体进行验证。 我想知道是对所有验证使用一个 validator 更好,还是每次验证时实例化一
Controller : @RequestMapping(...) public void foo(@Valid Parent p){ } class Parent { @NotNull // j
在 login.phtml 中,validator 和 validate 之间有什么区别 - 有人可以解释下面每一行的含义 function onepageLogin(button)
我有一个 java bean 用于将 JSON 消息发送到 spring @RestController 并且我有 bean 验证设置并使用 @Valid 运行得很好。但我想转移到 Protobuf/
我正在使用 vee-validate 来验证注册表单,我编写了如下代码,
使用 有什么区别属性和 标签? 我应该什么时候使用哪一种? 最佳答案 validator属性允许您引用独立的托管 bean 方法,而不仅仅是像这样的验证器
我们有这个 Alamofire 错误处理: Alamofire.request(.GET, getUrl("mystuff")).responseData { response in guar
以下代码创建了两个单选按钮。每个选项都包含一个日期值,该日期值已成功转换为格式为“yyyy-MM-dd”的标签。一旦我做出选择并单击下一步按钮,我就会收到以下错误“j_idt12:comDateCho
是否有类似的验证: req.checkBody('property', 'should be present').isPresent(); 值本身并不重要。 现在我使用这个解决方案: .isLengt
系列文章: 1、async-validator 源码学习(一):文档翻译 2、async-validator 源码学习笔记(二):目录结构 3、async-validator 源码学习笔记(三):ru
我正在使用 Backbone.Validation插入。我有一个模型可以在服务器端进行唯一性检查。我如何让 backbone.validation 识别错误并运行我设置的回调以显示返回的错误消息? t
我有一个 Knockout.Validation 场景,我认为该场景相当普遍,但尚未通过在网络上搜索和本网站上的各种答案找到解决方案。 我正在验证的属性在 ajax 调用之后才会添加到可观察到的 kn
我是 Knockout JS 的新手。我需要一个验证器来验证用户将在文本框中输入的日期。为此编写了如下代码 ko.validation.rules['date'] = { validator:
如何在 JSF 验证器中比较两个字符串是否相等? if (!settingsBean.getNewPassword().equals(settingsBean.getConfirmPassword()
我有简单的域: package app class Customers { String CUSTOMER String NOTE static mapping = {
我正在使用 ember-validations 来验证表单中的模型。 如果我使用 createRecord 创建记录,则模型的实例已经过验证,因此在用户输入值之前表单已经显示验证错误。 我只想在提交表
我有两种不同的服务:第一个是将对象保存到数据库,第二个是更新现有对象。 我正在对我的对象使用验证约束,例如(@NotBlank、@Size、@Pattern 等),在第一种情况下,我需要验证对象的所有
我有 2 个 ensure - 验证装饰器用于 2 个字段:password 和 retypePassword。我想在填写 retypePassword 字段时检查这两个字段是否相等。 问题是,当我在
I'm aware that it's a bug ,但是在域类上调用 validate() 会覆盖之前放入的任何拒绝: def save = { def assignment = new A
我是一名优秀的程序员,十分优秀!