JavaScript |基于自定义模式的对象的验证结构
处理需要验证传入请求对象的 Strucutre 的API。根据对类似问题的其他答案,我试图创建一个似乎无法正常工作的解决方案。
到目前为止,我的代码:
const testObj = {
"uuid": {
"value": "5481da7-8b7-22db-d326-b6a0a858ae2f",
"type": "id"
},
"rate": {
"value": 0.12,
"type": "percent"
}
}
let ok = true;
ok = testObj === OBJECT_SCHEMA; // is false. Expected to be true
我的验证架构:
const OBJECT_SCHEMA = {
"uuid": {
"value": String,
"type": String
},
"rate": {
"value": Number,
"type": String
}
}
Aynone可以指出我在这里做错了什么吗?
Working on a API that needs to validate the strucutre of the incoming request object. Based on other answers to similar questions, I have tried to create a solution that doesn't seem to be working.
My code so far:
const testObj = {
"uuid": {
"value": "5481da7-8b7-22db-d326-b6a0a858ae2f",
"type": "id"
},
"rate": {
"value": 0.12,
"type": "percent"
}
}
let ok = true;
ok = testObj === OBJECT_SCHEMA; // is false. Expected to be true
My validation schema:
const OBJECT_SCHEMA = {
"uuid": {
"value": String,
"type": String
},
"rate": {
"value": Number,
"type": String
}
}
Can aynone pinpoint what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让您开始的东西:
Something to get you started:
testObj === object_schema
是返回false,因为testobj和object_schema类型的值相等,但不是值。我的建议是结帐 yup or or zod 用于创建验证
testObj === OBJECT_SCHEMA
is returning false because the value of testObj and OBJECT_SCHEMA types are equal but not values.My recommendation would be to checkout Yup or Zod for creating a validation