Mongoose – 必需验证器未在函数中运行

huangapple go评论97阅读模式
英文:

Mongoose - required validator not running by function

问题

以下是您要翻译的内容:

我将一个函数传递给了mongoose模式中的required验证器,但如果我不传递任何值给字段,它不会触发。

在文档中说:

验证器不会在未定义的值上运行。唯一的例外是required验证器。

参考链接:https://mongoosejs.com/docs/validation.html

如果我传递一个函数,它不会应用吗?

编辑 - 共享代码:

我的字段定义:

field: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Model',
      required: !isBlock,
      autopopulate: true
    }

未触发的函数:

function isBlock() {
  return this.type === 'block'
}
英文:

I'm passing a function to required validator in mongoose schema, but it's not firing if i don't pass any value to the field.

In documentation says the following:

> Validators are not run on undefined values. The only exception is the required validator.

Reference: https://mongoosejs.com/docs/validation.html

If I pass a function is it not applied?

EDIT - Sharing the code:

My field definition:

<!-- language: lang-js -->

field: {
      type: mongoose.Schema.Types.ObjectId,
      ref: &#39;Model&#39;,
      required: !isBlock,
      autopopulate: true
    }

<!-- end snippet -->

The function that isn't firing:

<!-- language: lang-js -->

function isBlock() {
  return this.type === &#39;block&#39;
}

答案1

得分: 1

问题出在函数调用中的'!'操作符上,将代码更改为下面这样,我解决了我的问题。

required: function () { return !isBlock(this.type) }
英文:

Debugging I saw that the problem was with the '!' operator in the function call, changing the code to the under I solved my problem.

required: function () { return !isBlock(this.type) }

答案2

得分: 0

以下是您提供的代码的中文翻译:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const cryptoRandomString = require('crypto-random-string');

const userSchema = new Schema({
    firstName: {
        trim: true,
        type: String,
        required: [true, "必须提供firstName!"],
        validate(value) {
            if (value.length < 2) {
                throw new Error("firstName 无效!");
            }
        }
    },
    lastName: {
        trim: true,
        type: String,
        required: [true, "必须提供lastName!"],
        validate(value) {
            if (value.length < 2) {
                throw new Error("lastName 无效!");
            }
        }
    },
    email: {
        unique: [true, "电子邮件已注册"],
        type: String,
        required: [true, "必须提供电子邮件"]
    },
    mobile: {
        unique: [true, "手机号码已存在"],
        type: String,
        required: [true, "必须提供手机号码"],
        validate(value) {
            if (value.length !== 10) {
                throw new Error("手机号码无效!");
            }
        }
    },
    password: {
        type: String,
        required: [true, "必须提供密码"],
        validate(value) {
            if (value.length < 6) {
                throw new Error("密码必须至少为6个字符!");
            }
        }
    }
});

请注意,我已将代码中的错误消息翻译成中文。

英文:

I am posting a reference code match with this otherwise Show your code.

const mongoose = require(&quot;mongoose&quot;);
const Schema = mongoose.Schema;
const bcrypt = require(&quot;bcryptjs&quot;);
const jwt = require(&quot;jsonwebtoken&quot;);
const cryptoRandomString = require(&#39;crypto-random-string&#39;);
const userSchema = new Schema({
firstName: {
trim: true,
type: String,
required: [true, &quot;firstName is required!&quot;],
validate(value) {
if (value.length &lt; 2) {
throw new Error(&quot;firstName is invalid!&quot;);
}
}
},
lastName: {
trim: true,
type: String,
required: [true, &quot;lastName is required!&quot;],
validate(value) {
if (value.length &lt; 2) {
throw new Error(&quot;lastName is invalid!&quot;);
}
}
},
email: {
unique: [true, &quot;Email already registered&quot;],
type: String,
required: [true, &quot;Email is required&quot;]
},
mobile: {
unique: [true, &quot;Mobile Number alraedy available&quot;],
type: String,
required: [true, &quot;Mobile Number is required&quot;],
validate(value) {
if (value.length !== 10) {
throw new Error(&quot;Mobile Number is invalid!&quot;);
}
}
},
password: {
type: String,
required: [true, &quot;Password is required&quot;],
validate(value) {
if (value.length &lt; 6) {
throw new Error(&quot;Password must be atleast 6 characters!&quot;);
}
}
}
});

huangapple
  • 本文由 发表于 2020年1月6日 22:39:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定