Archived
2

allow null values for filters

This commit is contained in:
VLE2FE 2020-08-17 14:48:55 +02:00
parent a75d2a3424
commit fd5c0d2c74
2 changed files with 16 additions and 7 deletions

View File

@ -229,7 +229,10 @@ router.get('/samples', async (req, res, next) => {
pipeline: [{$match: {$expr: {$and: [ pipeline: [{$match: {$expr: {$and: [
{$eq: ['$sample_id', '$$sId']}, {$eq: ['$sample_id', '$$sId']},
{$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]} {$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}
]}}}], ]}}},
{$project: filters.fields.filter(e => /measurements\./.test(e))
.map(e => 'values.' + e.replace('measurements.', '')).reduce((s, e) => {s[e] = true; return s; }, {})}
],
as: 'measurements' as: 'measurements'
}}); }});
const groupedMeasurementTemplates = measurementTemplates.reduce((s, e) => { const groupedMeasurementTemplates = measurementTemplates.reduce((s, e) => {
@ -324,7 +327,10 @@ router.get('/samples', async (req, res, next) => {
pipeline: [{$match: {$expr: {$and: [ pipeline: [{$match: {$expr: {$and: [
{$eq: ['$sample_id', '$$sId']}, {$eq: ['$sample_id', '$$sId']},
{$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]} {$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]}
]}}}], ]}}},
{$project: filters.fields.filter(e => /measurements\./.test(e))
.map(e => 'values.' + e.replace('measurements.', '')).reduce((s, e) => {s[e] = true; return s; }, {})}
],
as: 'measurements' as: 'measurements'
}}); }});
} }

View File

@ -195,6 +195,9 @@ export default class SampleValidate {
// data.filters[i] = JSON.parse(decodeURIComponent(data.filters[i])); // data.filters[i] = JSON.parse(decodeURIComponent(data.filters[i]));
data.filters[i] = JSON.parse(data.filters[i]); data.filters[i] = JSON.parse(data.filters[i]);
data.filters[i].values = data.filters[i].values.map(e => { // validate filter values data.filters[i].values = data.filters[i].values.map(e => { // validate filter values
if (e === null) { // null values are always allowed
return null;
}
let validator; let validator;
let field = data.filters[i].field let field = data.filters[i].field
if (/material\./.test(field)) { // select right validation model if (/material\./.test(field)) { // select right validation model
@ -253,7 +256,7 @@ export default class SampleValidate {
new RegExp('^(' + this.fieldKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')$', 'm') new RegExp('^(' + this.fieldKeys.join('|').replace(/\./g, '\\.').replace(/\*/g, '.+') + ')$', 'm')
).messages({'string.pattern.base': 'Invalid filter field name'}), ).messages({'string.pattern.base': 'Invalid filter field name'}),
values: Joi.array().items(Joi.alternatives().try( values: Joi.array().items(Joi.alternatives().try(
Joi.string().max(128), Joi.number(), Joi.boolean(), Joi.date().iso(), Joi.object() Joi.string().max(128), Joi.number(), Joi.boolean(), Joi.date().iso(), Joi.object(), null
)).min(1) )).min(1)
})).default([]) })).default([])
}).with('to-page', 'page-size').validate(data); }).with('to-page', 'page-size').validate(data);