Skip to content

Commit

Permalink
feat: Add ability for users to requests using the subfields values (#477
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Aviortheking authored Jan 22, 2024
1 parent 2cfa860 commit 41bf9af
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .bruno/fixes/467-options-not-working-correctly.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: 467 - Validate that we can run OPTIONS
type: http
seq: 3
seq: 2
}

options {
Expand Down
2 changes: 1 addition & 1 deletion .bruno/fixes/471-invalid-set-sorting.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: 471 - Invalid Set Sorting
type: http
seq: 2
seq: 3
}

get {
Expand Down
25 changes: 25 additions & 0 deletions .bruno/fixes/475-ability-query-subfields.bru
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
meta {
name: 475 - Ability to query subfileds
type: http
seq: 4
}

get {
url: {{BASE_URL}}/v2/en/cards?legal.standard=true
body: none
auth: none
}

query {
legal.standard: true
}

assert {
res.status: eq 200
}

docs {
Validate the issue seen in

https://github.com/tcgdex/cards-database/issues/474
}
37 changes: 33 additions & 4 deletions server/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { objectLoop } from '@dzeio/object-util'
import { mustBeObject, objectLoop } from '@dzeio/object-util'
import { SupportedLanguages } from '@tcgdex/sdk'
import { Response } from 'express'
import { Query } from './interfaces'
Expand Down Expand Up @@ -211,11 +211,41 @@ export function handleValidation(data: Array<any>, query: Query) {
return data
}

return data.filter((v) => objectLoop(filters, (valueToValidate, key) => {
return validateItem(valueToValidate, v[key], query.strict)
return data.filter((v) => objectLoop(filters, (valueToValidate, key: string) => {
let value: any
// handle subfields
if (key.includes('.')) {
value = objectGet(v, key.split('.'))
} else {
value = v[key]
}
return validateItem(valueToValidate, value, query.strict)
}))
}

/**
* go through an object to get a specific value
* @param obj the object to go through
* @param path the path to follow
* @returns the value or undefined
*/
function objectGet(obj: object, path: Array<string | number | symbol>): any | undefined {
mustBeObject(obj)
let pointer: object = obj;
for (let index = 0; index < path.length; index++) {
const key = path[index];
const nextIndex = index + 1;
if (!Object.prototype.hasOwnProperty.call(pointer, key) && nextIndex < path.length) {
return undefined
}
// if last index
if (nextIndex === path.length) {
return (pointer as any)[key]
}
// move pointer to new key
pointer = (pointer as any)[key]
}
}

/**
* validate that the value is null or undefined
Expand All @@ -225,4 +255,3 @@ export function handleValidation(data: Array<any>, query: Query) {
function isNull(value: any): value is (undefined | null) {
return typeof value === 'undefined' || value === null
}

0 comments on commit 41bf9af

Please sign in to comment.