我正在将IOS应用程序中的图像上传到Firebase,Firebase返回包括类型为URL
的URL在内的元数据。
我应该将它存储为以下代码中的String
类型,还是有特定的类型适用于URL
?
var schema = new Schema({
downloadURL: { type: String, createdDate: Date.now }
})
我正在将IOS应用程序中的图像上传到Firebase,Firebase返回包括类型为URL
的URL在内的元数据。
我应该将它存储为以下代码中的String
类型,还是有特定的类型适用于URL
?
var schema = new Schema({
downloadURL: { type: String, createdDate: Date.now }
})
const mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
downloadURL: {
type: String,
required: 'URL can\'t be empty',
unique: true
},
description: {
type: String,
required: 'Description can\'t be empty',
}
});
userSchema.path('downloadURL').validate((val) => {
urlRegex = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/;
return urlRegex.test(val);
}, 'Invalid URL.');
Mongoose没有URL的模式,您可以将其存储在字符串中,并使用mongoose-Validator进行验证。
以下是它的语法:
validate: {
validator: value => validator.isURL(value, { protocols: ['http','https','ftp'], require_tld: true, require_protocol: true }),
message: 'Must be a Valid URL'
}