From fd5c0d2c74fb40c206daabad9d4a39fe0c1a09d3 Mon Sep 17 00:00:00 2001 From: VLE2FE Date: Mon, 17 Aug 2020 14:48:55 +0200 Subject: [PATCH] allow null values for filters --- src/routes/sample.ts | 18 ++++++++++++------ src/routes/validate/sample.ts | 5 ++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/routes/sample.ts b/src/routes/sample.ts index 4fe1763..adb645a 100644 --- a/src/routes/sample.ts +++ b/src/routes/sample.ts @@ -227,9 +227,12 @@ router.get('/samples', async (req, res, next) => { queryPtr.push({$lookup: { from: 'measurements', let: {sId: '$_id'}, pipeline: [{$match: {$expr: {$and: [ - {$eq: ['$sample_id', '$$sId']}, - {$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]} - ]}}}], + {$eq: ['$sample_id', '$$sId']}, + {$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' }}); const groupedMeasurementTemplates = measurementTemplates.reduce((s, e) => { @@ -322,9 +325,12 @@ router.get('/samples', async (req, res, next) => { queryPtr.push({$lookup: { from: 'measurements', let: {sId: '$_id'}, pipeline: [{$match: {$expr: {$and: [ - {$eq: ['$sample_id', '$$sId']}, - {$in: ['$measurement_template', measurementTemplates.map(e => mongoose.Types.ObjectId(e._id))]} - ]}}}], + {$eq: ['$sample_id', '$$sId']}, + {$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' }}); } diff --git a/src/routes/validate/sample.ts b/src/routes/validate/sample.ts index 665a01b..a204e6c 100644 --- a/src/routes/validate/sample.ts +++ b/src/routes/validate/sample.ts @@ -195,6 +195,9 @@ export default class SampleValidate { // data.filters[i] = JSON.parse(decodeURIComponent(data.filters[i])); data.filters[i] = JSON.parse(data.filters[i]); 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 field = data.filters[i].field 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') ).messages({'string.pattern.base': 'Invalid filter field name'}), 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) })).default([]) }).with('to-page', 'page-size').validate(data);