发送多个子文档时,Mongoose 验证失败

Posted

技术标签:

【中文标题】发送多个子文档时,Mongoose 验证失败【英文标题】:Mongoose validation fails when more than one subdocument is sent 【发布时间】:2019-12-09 09:53:07 【问题描述】:

我正在设置一个新服务器,并创建了一个名为 Announcement 的 mongoose 模型,它有一个名为 Image 的子文档数组。每当我向创建发送超过 1 个子文档时,它都会通过验证来检查图像的 url 是否与正则表达式匹配。有时第二个子文档会失败,有时会失败。

regex.js:

export const EMAIL_REGEX = /^(([^<>()\[\]\\.,:\s@"]+(\.[^<>()\[\]\\.,:\s@"]+)*)|(".+"))@((\[[0-9]1,3\.[0-9]1,3\.[0-9]1,3\.[0-9]1,3])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]2,))$/
export const FULL_NAME_REGEX = /^[A-Za-zÀ-ÖØ-öø-ÿ]+(([',. -][A-Za-zÀ-ÖØ-öø-ÿ ])?[A-Za-zÀ-ÖØ-öø-ÿ]*)*$/
export const URL_REGEX = /(https?:)?\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]2,256\.[a-z]2,4\b([-a-zA-Z0-9@:%_+.~#?&\/=]*)/gi

image.js:

import mongoose from 'mongoose'

import  URL_REGEX  from '../../../config/regex'

const ImageSchema = new mongoose.Schema(
  url: 
    type: String,
    required: [true, 'Url is required'],
    match: [URL_REGEX, 'Invalid url']
  ,
  alt: 
    type: String,
    required: [true, 'Alt is required']
  
)

export default ImageSchema

announcement.js:

import mongoose        from 'mongoose'
import uniqueValidator from 'mongoose-unique-validator'

import ImageSchema         from './image'
import  FULL_NAME_REGEX  from '../../../config/regex'

const AnnouncementSchema = new mongoose.Schema(
  author: 
    type: String,
    required: [true, 'Author is required'],
    match: [FULL_NAME_REGEX, 'Invalid name']
  ,
  title: 
    type: String,
    required: [true, 'Title is required']
  ,
  content: 
    type: String,
    required: [true, 'Content is required']
  ,
  images: 
    type: [ImageSchema],
    default: []
  ,
  createdAt: 
    required: true,
    type: Date
  ,
  updatedAt: 
    required: true,
    type: Date
  
)

ModelSchema.plugin(uniqueValidator, 
  message: 'PATH (VALUE) needs to be unique'
)

AnnouncementSchema.pre('validate', function(next) 
  const date = new Date()

  if (this.isNew) 
    this.createdAt = date
  

  this.updatedAt = date

  next()
)

export default mongoose.model('announcements', AnnouncementSchema)

如果我尝试通过以下方式创建公告:

new Announcement(
  author: "John Doe",
  title: "Lorem Ipsum",
  content: "Lorem ipsum dolor sit amet",
  images: [
    url: "http://google.com/image1.jpg",
    alt: "Image description"
  ]
).save()

它按预期工作。

但如果我尝试使用:

new Announcement(
  author: "John Doe",
  title: "Lorem Ipsum",
  content: "Lorem ipsum dolor sit amet",
  images: [
    url: "http://google.com/image1.jpg",
    alt: "Image description"
  , 
    url: "http://google.com/image2.jpg",
    alt: "Image description"
  ]
).save()

我明白了:


  "images.1.url": "Invalid url"

有人经历过吗?

【问题讨论】:

好发现!您可以将自己的解决方案作为答案并接受它以结束问题。它是否适用于 2 张以上的图片? @Michael 完成!是的,它确实适用于超过 2 张图片。 【参考方案1】:

原来是正则表达式,由于某种原因它不能处理多个图像...这很奇怪,因为它们是单独验证的。

新的正则表达式是:

/((([A-Za-z]3,9:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-_]*)?\??(?:[\-\+=&;%@\.\w_]*)#?(?:[\.\!\/\\\w]*))?)/

【讨论】:

以上是关于发送多个子文档时,Mongoose 验证失败的主要内容,如果未能解决你的问题,请参考以下文章

Mongoose 验证子文档数组

Mongoose - 验证不同子文档类型的数组

Mongoose.js getter 在 Find() 上膨胀子文档?

Mongoose 所需的验证器失败值未定义

Mongoose 所需的验证器失败值未定义

安装oracle数据库时验证失败,急求大神啊