diff --git a/.bruno/fixes/474-queries-crashing-the-server.bru b/.bruno/fixes/474-queries-crashing-the-server.bru new file mode 100644 index 000000000..4c4ae04eb --- /dev/null +++ b/.bruno/fixes/474-queries-crashing-the-server.bru @@ -0,0 +1,25 @@ +meta { + name: 474 - Queries crashing the server + type: http + seq: 2 +} + +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 +} diff --git a/server/src/util.ts b/server/src/util.ts index 8c7a62bf9..45001b1d9 100644 --- a/server/src/util.ts +++ b/server/src/util.ts @@ -68,6 +68,11 @@ export function validateItem(validator: any | Array, value: any, strict: bo validator = parseInt(validator) } + // do a comparaison with null values + if (isNull(value)) { + return isNull(validator) + } + if (typeof value === 'object') { // invert signal so that an early exit mean that a match was found! return !objectLoop(value, (v) => { @@ -210,3 +215,14 @@ export function handleValidation(data: Array, query: Query) { return validateItem(valueToValidate, v[key], query.strict) })) } + + +/** + * validate that the value is null or undefined + * @param value the value the check + * @returns if the value is undefined or null or not + */ +function isNull(value: any): value is (undefined | null) { + return typeof value === 'undefined' || value === null +} +