1
0
mirror of https://github.com/tcgdex/cards-database.git synced 2025-04-23 19:32:11 +00:00

feat: Add ability for users to requests using the subfields values (#477)

This commit is contained in:
Florian Bouillon 2024-01-22 01:48:04 +01:00 committed by GitHub
parent 2cfa860f6d
commit 41bf9afde7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 60 additions and 6 deletions

View File

@ -1,7 +1,7 @@
meta { meta {
name: 467 - Validate that we can run OPTIONS name: 467 - Validate that we can run OPTIONS
type: http type: http
seq: 3 seq: 2
} }
options { options {

View File

@ -1,7 +1,7 @@
meta { meta {
name: 471 - Invalid Set Sorting name: 471 - Invalid Set Sorting
type: http type: http
seq: 2 seq: 3
} }
get { get {

View File

@ -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
}

View File

@ -1,4 +1,4 @@
import { objectLoop } from '@dzeio/object-util' import { mustBeObject, objectLoop } from '@dzeio/object-util'
import { SupportedLanguages } from '@tcgdex/sdk' import { SupportedLanguages } from '@tcgdex/sdk'
import { Response } from 'express' import { Response } from 'express'
import { Query } from './interfaces' import { Query } from './interfaces'
@ -211,11 +211,41 @@ export function handleValidation(data: Array<any>, query: Query) {
return data return data
} }
return data.filter((v) => objectLoop(filters, (valueToValidate, key) => { return data.filter((v) => objectLoop(filters, (valueToValidate, key: string) => {
return validateItem(valueToValidate, v[key], query.strict) 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 * validate that the value is null or undefined
@ -225,4 +255,3 @@ export function handleValidation(data: Array<any>, query: Query) {
function isNull(value: any): value is (undefined | null) { function isNull(value: any): value is (undefined | null) {
return typeof value === 'undefined' || value === null return typeof value === 'undefined' || value === null
} }