Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 74 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,21 @@ function attachQueries (request) {
const field = inflect(matches[1])
const filterType = matches[2]

if (!(field in fields)) throw new BadRequestError(
`The field "${field}" is non-existent.`)
if(isRelationFilter(field)){
const relationPath = field;
if(filterType !== 'fuzzy-match'){
throw new BadRequestError('Filtering relationship only supported on fuzzy-match for now')
}
console.log('Checking path')
if(! isValidRelationPath( recordTypes, fields, getRelationFilterSegments(field) ) ){
throw new BadRequestError(`Path ${relationPath} is not valid`)
}
}

else if (!(field in fields))
throw new BadRequestError(`The field "${field}" is non-existent.`)

const fieldType = fields[field][keys.type]
const fieldType = getLastTypeInPath( recordTypes, fields, getRelationFilterSegments(field) )[keys.type]

if (filterType === void 0) {
if (!('match' in request.options)) request.options.match = {}
Expand All @@ -353,6 +364,20 @@ function attachQueries (request) {
request.options.range[field][index] =
castValue(query[parameter], fieldType, options)
}
else if (filterType === 'fuzzy-match'){

if ( ! getLastTypeInPath( recordTypes, fields, getRelationFilterSegments(field) )[keys.type] ){
throw new BadRequestError( `fuzzy-match only allowed on attributes. For ${field}` )
}

if ( getLastTypeInPath( recordTypes, fields, getRelationFilterSegments(field) )[keys.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.`)
}
Expand Down Expand Up @@ -515,3 +540,49 @@ function setInflectType (inflect, types) {

return out
}

function isValidRelationPath( recordTypes, fieldsCurrentType, remainingPathSegments ){
if( !remainingPathSegments.length ){
return false
}
const pathSegment = remainingPathSegments[0]

if( !fieldsCurrentType[pathSegment] ){
return false
}
if( fieldsCurrentType[pathSegment].type && remainingPathSegments.length == 1 ){
return true
}

const nextTypeToCompare = fieldsCurrentType[pathSegment].link
if( nextTypeToCompare ){
return isValidRelationPath( recordTypes, recordTypes[nextTypeToCompare], remainingPathSegments.slice(1) )
}
return false
}

function getLastTypeInPath( recordTypes, fieldsCurrentType, remainingPathSegments ){
if( !remainingPathSegments.length ){
return fieldsCurrentType
}

const pathSegment = remainingPathSegments[0]

if( !fieldsCurrentType[pathSegment] ){
return {}
}

const nextType = fieldsCurrentType[pathSegment].link
if( nextType ){
return getLastTypeInPath( recordTypes, recordTypes[nextType], remainingPathSegments.slice(1) )
}
return fieldsCurrentType[pathSegment]
}

function isRelationFilter( field ){
return field.split(':').length > 1
}

function getRelationFilterSegments( field ){
return field.split(':')
}