( queryPart: Record<string, unknown>, entityName: string, schema: IContainEntitiesAndLinks<any, any> | null | undefined, path: string, depth: number = 0, )
| 416 | }; |
| 417 | |
| 418 | const validateEntityInQuery = ( |
| 419 | queryPart: Record<string, unknown>, |
| 420 | entityName: string, |
| 421 | schema: IContainEntitiesAndLinks<any, any> | null | undefined, |
| 422 | path: string, |
| 423 | depth: number = 0, |
| 424 | ): void => { |
| 425 | if (!queryPart || typeof queryPart !== 'object') { |
| 426 | throw new QueryValidationError( |
| 427 | `Query part for entity '${entityName}' must be an object, but received: ${typeof queryPart}`, |
| 428 | path, |
| 429 | ); |
| 430 | } |
| 431 | |
| 432 | for (const key of Object.keys(queryPart)) { |
| 433 | if (key !== '$') { |
| 434 | // Validate link exists |
| 435 | if (schema && !(key in schema.entities[entityName].links)) { |
| 436 | const availableLinks = Object.keys(schema.entities[entityName].links); |
| 437 | throw new QueryValidationError( |
| 438 | `Link '${key}' does not exist on entity '${entityName}'. Available links: ${availableLinks.length > 0 ? availableLinks.join(', ') : 'none'}`, |
| 439 | `${path}.${key}`, |
| 440 | ); |
| 441 | } |
| 442 | |
| 443 | // Recursively validate nested query |
| 444 | const nestedQuery = queryPart[key]; |
| 445 | if (typeof nestedQuery === 'object' && nestedQuery !== null) { |
| 446 | const linkedEntityName = |
| 447 | schema?.entities[entityName].links[key]?.entityName; |
| 448 | if (linkedEntityName) { |
| 449 | validateEntityInQuery( |
| 450 | nestedQuery as Record<string, unknown>, |
| 451 | linkedEntityName, |
| 452 | schema, |
| 453 | `${path}.${key}`, |
| 454 | depth + 1, |
| 455 | ); |
| 456 | } |
| 457 | } |
| 458 | } else { |
| 459 | // Validate $ object |
| 460 | const dollarObj = queryPart[key]; |
| 461 | if (typeof dollarObj !== 'object' || dollarObj === null) { |
| 462 | throw new QueryValidationError( |
| 463 | `Query parameter '$' must be an object in entity '${entityName}', but received: ${typeof dollarObj}`, |
| 464 | `${path}.$`, |
| 465 | ); |
| 466 | } |
| 467 | |
| 468 | validateDollarObject( |
| 469 | dollarObj as Record<string, unknown>, |
| 470 | entityName, |
| 471 | schema, |
| 472 | `${path}.$`, |
| 473 | depth, |
| 474 | ); |
| 475 | } |
no test coverage detected