From e79ed3d292a7d468fe182b3593d2c5e4a55a0f31 Mon Sep 17 00:00:00 2001 From: Felix Ruiz de Arcaute Date: Fri, 8 May 2020 18:27:26 +0200 Subject: [PATCH] Attempt to allow fuzzy-match filter in json-api Notes ----- Regarding integration into fortune.js I am unsure what field of request.options may be used. As I understand it, Adapter.find leaves the possibility of extra fields on the options object unspecified. Therefore, I added the fuzzyMatch on the ptions object. Regarding validity JSON:API Filtering is relatively unspecified. As such, I assume the extra filter type is legal. --- lib/helpers.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/helpers.js b/lib/helpers.js index 1ade979..cf38a8d 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -353,6 +353,20 @@ function attachQueries (request) { request.options.range[field][index] = castValue(query[parameter], fieldType, options) } + else if (filterType === 'fuzzy-match'){ + + if ( ! fields[field].type ){ + throw new BadRequestError( `fuzzy-match only allowed on attributes.` ) + } + + if ( fields[field].type.name !== "String"){ + throw new BadRequestError( + `fuzzy-match only allowed on String types. ${field} is of type ${fields[field].type.name }` ) + } + + if (!('fuzzyMatch' in request.options)) request.options['fuzzyMatch'] = {} + request.options.fuzzyMatch[field] = query[parameter] + } else throw new BadRequestError( `The filter "${filterType}" is not valid.`) }