mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-21 01:21:28 -08:00
cfe9525dd4
also refactored the code to 1. stop passing around `scope === 'global'`, since this code can be used only for changing globalRole. 2. leak less details when input validation fails. ## Review / Merge checklist - [x] PR title and summary are descriptive - [x] Tests included
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import type express from 'express';
|
|
import { validate } from 'class-validator';
|
|
import type { WorkflowEntity } from '@db/entities/WorkflowEntity';
|
|
import type { CredentialsEntity } from '@db/entities/CredentialsEntity';
|
|
import type { TagEntity } from '@db/entities/TagEntity';
|
|
import type { User } from '@db/entities/User';
|
|
import type { UserRoleChangePayload, UserUpdatePayload } from '@/requests';
|
|
import { BadRequestError } from './errors/response-errors/bad-request.error';
|
|
|
|
/**
|
|
* Returns the session id if one is set
|
|
*/
|
|
export function getSessionId(req: express.Request): string | undefined {
|
|
return req.headers.sessionid as string | undefined;
|
|
}
|
|
|
|
export async function validateEntity(
|
|
entity:
|
|
| WorkflowEntity
|
|
| CredentialsEntity
|
|
| TagEntity
|
|
| User
|
|
| UserUpdatePayload
|
|
| UserRoleChangePayload,
|
|
): Promise<void> {
|
|
const errors = await validate(entity);
|
|
|
|
const errorMessages = errors
|
|
.reduce<string[]>((acc, cur) => {
|
|
if (!cur.constraints) return acc;
|
|
acc.push(...Object.values(cur.constraints));
|
|
return acc;
|
|
}, [])
|
|
.join(' | ');
|
|
|
|
if (errorMessages) {
|
|
throw new BadRequestError(errorMessages);
|
|
}
|
|
}
|
|
|
|
export const DEFAULT_EXECUTIONS_GET_ALL_LIMIT = 20;
|