我有这个非常基本的模型只有一个字段name
,我想要对正则表达式进行验证:
const Projects = new ProjectsCollection('projects');
Projects.schema = new SimpleSchema({
_id : {type: String, regEx: SimpleSchema.RegEx.Id},
name : {
type : String,
regEx: /^[a-zA-Z0-9]+((\s[a-zA-Z0-9]+)|(_[a-zA-Z0-9]+)|(-[a-zA-Z0-9]+)|(\.[a-zA-Z0-9]+))?$/
}
});
Projects.attachSchema(Projects.schema);
当验证失败时,我回到验证错误,说regex failed for Name
这对我来说是不合需要的,因为它不明确,用户不知道我究竟需要他输入什么。
我尝试添加以下内容但没有成功:
Projects.schema.messages({
"regEx name": [{
msg: "test error message"
}]
});
然而,这个..工作,但问题是我可以有任何其他模型与名称字段,它将为所有这些都吐出相同的错误消息(我计划另一个模型与{{1 }}}:
name
我也试过(没有成功):
SimpleSchema.messages({
"regEx name": [{
msg: "test error message"
}]
});
我通过方法插入,这是我的插入代码:
SimpleSchema.messages({
"regEx projects.name": [{
msg: "test error message"
}]
});
关于我应该如何配置验证消息以便我可以针对特定字段定位它们的任何想法?
答案 0 :(得分:1)
有关SELECT p.id_product AS 'ID',
pl.id_lang AS 'ID_LANG',
p.active AS 'Active (0/1)',
pl.name AS 'Name',
p.id_category_default AS 'Default Category',
p.price AS 'Price tax excl.',
p.id_tax_rules_group AS 'Tax rules ID',
p.wholesale_price AS 'Wholesale price',
p.on_sale AS 'On sale (0/1)',
p.reference AS 'Reference #',
p.quantity AS 'Quantity',
pl.description_short AS 'Short description',
pl.description AS 'Description',
pl.meta_title AS 'Meta-title',
pl.meta_keywords AS 'Meta-keywords',
pl.meta_description AS 'Meta-description',
pl.link_rewrite AS 'URL rewritten',
pl.available_now AS 'Text when in stock',
pl.available_later AS 'Text when backorder allowed',
p.available_for_order AS 'Available for order',
p.date_add AS 'Product creation date',
p.show_price AS 'Show price',
p.online_only AS 'Available online only',
p.condition AS 'Condition',
concat( 'http://YOUR-URL.com/img/p/',mid(im.id_image,1,1),'/', if (length(im.id_image)>1,concat(mid(im.id_image,2,1),'/'),''),if (length(im.id_image)>2,concat(mid(im.id_image,3,1),'/'),''),if (length(im.id_image)>3,concat(mid(im.id_image,4,1),'/'),''),if (length(im.id_image)>4,concat(mid(im.id_image,5,1),'/'),''), im.id_image, '.jpg' ) AS url_image
FROM ps_product p
INNER JOIN ps_product_lang pl ON p.id_product = pl.id_product
LEFT JOIN ps_image im ON p.id_product = im.id_product
WHERE 1=1
and p.active = 1
消息的特殊情况,请参阅the documentation:
在你的情况下,你应该尝试:
regex
答案 1 :(得分:0)
此答案和问题适用于流星-简单模式的v1
我遇到了同样的问题,这对我有用。我认为自定义消息是基于实例的-仅绑定到一个simplSchema实例。然后,在您的经过验证的方法中,pick()将创建一个新的SimplSchema实例,而不包含您的自定义消息。
我需要从“父”模式手动添加自定义消息。 在这种情况下:
const insertProjectSchema = Projects.schema.pick('name');
insertProjectSchema.messages(Projects.schema._messages);
export const insert = new ValidatedMethod({
name : 'projects.insert',
mixins : [simpleSchemaMixin],
schema : insertProjectSchema,
schemaValidatorOptions: {
clean : true,
filter: false
},
run({name}) {
return Projects.insert({
name
}, null);
},
});