首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何知道模式中的所有字段是否都已填充?

问如何知道模式中的所有字段是否都已填充?
EN

Stack Overflow用户
提问于 2020-01-29 18:37:30
回答 1查看 86关注 0票数 0

我有一个模式,如下:

如何检查模式中的所有字段是否都已填充?

前端(react.js)有一个入职流程,在该流程中,他们需要首次登录的用户进入入职流程,第二次或多次登录的用户被发送到其他页面。因此,它们需要我提供一个布尔值。如何知道是否所有数据都已填充?

代码语言:javascript
复制
const userSchema = mongoose.Schema({
  phoneNumber: { type: String },
  role: { type: String, default: 'user' },
  userDetails: {
    firstName: { type: String },
    lastName: { type: String },
    userName: { type: String },
    password: { type: String },
    email: { type: String },
    street_1: { type: String },
    street_2: { type: String },
    landmark: { type: String },
    city: { type: String },
    state: { type: String },
    zipcode: { type: String },
  },
  kycDetails: {
    aadharNumber: { type: String },
    aadharFront: {
      type: String,
    },
    aadharBack: {
      type: String,
    },
    panNumber: { type: String },
    panImage: {
      type: String,
    },
  },
  bankDetails: {
    accountNumber: { type: String },
    accountName: { type: String },
    bankName: { type: String },
    ifscCode: { type: String },
  },
});
EN

回答 1

Stack Overflow用户

发布于 2020-01-29 18:48:20

您可以使用验证包https://ajv.js.org/来检查Json中需要的所有字段

例如

代码语言:javascript
复制
const nSchema = {
    type: 'object',
    properties: {
        phoneNumber: {type: 'string'},
        role: {type: 'string', enum:['user']},
    },
    required: ['phoneNumber', 'role'],
    additionalProperties: false
};
const valid = ajv.validate(nSchema, data);
if (!valid) {

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59965264

复制
相关文章

相似问题

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