mirror of
https://github.com/CaramelFur/Picsur.git
synced 2025-11-13 07:15:39 +01:00
relocate validator composer
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { SetMetadata, UseGuards } from '@nestjs/common';
|
import { SetMetadata, UseGuards } from '@nestjs/common';
|
||||||
import { CombineDecorators } from 'picsur-shared/dist/util/decorator';
|
import { CombineFCDecorators } from 'picsur-shared/dist/util/decorator';
|
||||||
import { LocalAuthGuard } from '../managers/auth/guards/localauth.guard';
|
import { LocalAuthGuard } from '../managers/auth/guards/localauth.guard';
|
||||||
import { Permissions } from '../models/dto/permissions.dto';
|
import { Permissions } from '../models/dto/permissions.dto';
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ export const RequiredPermissions = (...permissions: Permissions) => {
|
|||||||
export const NoPermissions = () => RequiredPermissions();
|
export const NoPermissions = () => RequiredPermissions();
|
||||||
|
|
||||||
export const UseLocalAuth = (...permissions: Permissions) =>
|
export const UseLocalAuth = (...permissions: Permissions) =>
|
||||||
CombineDecorators(
|
CombineFCDecorators(
|
||||||
RequiredPermissions(...permissions),
|
RequiredPermissions(...permissions),
|
||||||
UseGuards(LocalAuthGuard),
|
UseGuards(LocalAuthGuard),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
// This enum is only here to make accessing the values easier, and type checking in the backend
|
||||||
export enum SysPreference {
|
export enum SysPreference {
|
||||||
JwtSecret = 'jwt_secret',
|
JwtSecret = 'jwt_secret',
|
||||||
JwtExpiresIn = 'jwt_expires_in',
|
JwtExpiresIn = 'jwt_expires_in',
|
||||||
|
|||||||
@@ -11,9 +11,8 @@ export class EImage {
|
|||||||
|
|
||||||
// Binary data
|
// Binary data
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@Exclude()
|
@Exclude() // Dont send this by default
|
||||||
data?: object;
|
data?: object;
|
||||||
|
|
||||||
|
|
||||||
@IsNotEmpty()
|
@IsNotEmpty()
|
||||||
@IsString()
|
@IsString()
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ import { EntityID } from '../validators/entity-id.validator';
|
|||||||
import { IsRoleName } from '../validators/role.validators';
|
import { IsRoleName } from '../validators/role.validators';
|
||||||
import { IsStringList } from '../validators/string-list.validator';
|
import { IsStringList } from '../validators/string-list.validator';
|
||||||
|
|
||||||
|
// This entity is build from multiple smaller enitities
|
||||||
|
// Theses smaller entities are used in other places
|
||||||
|
|
||||||
export class RoleNameObject {
|
export class RoleNameObject {
|
||||||
@IsRoleName()
|
@IsRoleName()
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import { EntityID } from '../validators/entity-id.validator';
|
|||||||
import { IsStringList } from '../validators/string-list.validator';
|
import { IsStringList } from '../validators/string-list.validator';
|
||||||
import { IsPlainTextPwd, IsUsername } from '../validators/user.validators';
|
import { IsPlainTextPwd, IsUsername } from '../validators/user.validators';
|
||||||
|
|
||||||
|
// This entity is build from multiple smaller enitities
|
||||||
|
// Theses smaller entities are used in other places
|
||||||
|
|
||||||
export class UsernameUser {
|
export class UsernameUser {
|
||||||
@IsUsername()
|
@IsUsername()
|
||||||
username: string;
|
username: string;
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
// This is a simple wrapper for failures
|
||||||
|
// It makes it a lot more pleasant to work with errors
|
||||||
|
// Since now they dont just come out of nowhere
|
||||||
|
// -> Side effects go brrr
|
||||||
|
|
||||||
export class Failure {
|
export class Failure {
|
||||||
constructor(private readonly reason?: string) {}
|
constructor(private readonly reason?: string) {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
export * from './failable';
|
export * from './failable';
|
||||||
export * from './newable';
|
export * from './newable';
|
||||||
|
export * from './tuple';
|
||||||
|
|||||||
@@ -1 +1,3 @@
|
|||||||
|
// Any thing that can come after 'new'
|
||||||
|
|
||||||
export type Newable<T> = { new (...args: any[]): T };
|
export type Newable<T> = { new (...args: any[]): T };
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// Easily create a tuple with appropriate types
|
||||||
|
|
||||||
const tuple = <T extends string[]>(...args: T): T => args;
|
const tuple = <T extends string[]>(...args: T): T => args;
|
||||||
|
|
||||||
export default tuple;
|
export default tuple;
|
||||||
|
|||||||
@@ -1,9 +1,17 @@
|
|||||||
type FCDecorator = MethodDecorator & ClassDecorator;
|
type FCDecorator = MethodDecorator & ClassDecorator;
|
||||||
|
|
||||||
export function CombineDecorators(...decorators: FCDecorator[]) {
|
export function CombineFCDecorators(...decorators: FCDecorator[]) {
|
||||||
return (target: any, key: string, descriptor: PropertyDescriptor) => {
|
return (target: any, key: string, descriptor: PropertyDescriptor) => {
|
||||||
decorators.forEach(decorator => {
|
decorators.forEach(decorator => {
|
||||||
decorator(target, key, descriptor);
|
decorator(target, key, descriptor);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const CombinePDecorators = (...decorators: PropertyDecorator[]): () => PropertyDecorator => {
|
||||||
|
return () => {
|
||||||
|
return (target: Object, propertyKey: string | symbol): void => {
|
||||||
|
decorators.forEach((decorator) => decorator(target, propertyKey));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ const randomCharacters =
|
|||||||
export function generateRandomString(length: number): string {
|
export function generateRandomString(length: number): string {
|
||||||
let out = '';
|
let out = '';
|
||||||
for (let i = 0; i < length; i++) {
|
for (let i = 0; i < length; i++) {
|
||||||
|
// Yes this is done synchronously, but it's not a big deal
|
||||||
out += randomCharacters[crypto.randomInt(0, randomCharacters.length - 1)];
|
out += randomCharacters[crypto.randomInt(0, randomCharacters.length - 1)];
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
import { validate } from 'class-validator';
|
import { validate } from 'class-validator';
|
||||||
|
|
||||||
|
// For some stupid reason, the class-validator library does not have a way to set global defaults
|
||||||
|
// So now we have to do it this way
|
||||||
|
|
||||||
export const ValidateOptions = {
|
export const ValidateOptions = {
|
||||||
disableErrorMessages: true,
|
disableErrorMessages: true,
|
||||||
forbidNonWhitelisted: true,
|
forbidNonWhitelisted: true,
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
|
|
||||||
export const ComposeValidators = (...validators: PropertyDecorator[]): () => PropertyDecorator => {
|
|
||||||
return () => {
|
|
||||||
const decorators = [...validators];
|
|
||||||
|
|
||||||
return (target: Object, propertyKey: string | symbol): void => {
|
|
||||||
decorators.forEach((decorator) => decorator(target, propertyKey));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import { IsInt, IsNotEmpty, IsOptional, Min } from 'class-validator';
|
import { IsInt, IsNotEmpty, IsOptional, Min } from 'class-validator';
|
||||||
import { ComposeValidators } from './compose.validator';
|
import { CombinePDecorators } from '../util/decorator';
|
||||||
|
|
||||||
export const EntityID = ComposeValidators(IsOptional(), IsInt(), Min(0));
|
export const EntityID = CombinePDecorators(IsOptional(), IsInt(), Min(0));
|
||||||
export const EntityIDRequired = ComposeValidators(IsNotEmpty(), IsInt(), Min(0));
|
export const EntityIDRequired = CombinePDecorators(IsNotEmpty(), IsInt(), Min(0));
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { IsDefined, IsInt, Min } from 'class-validator';
|
import { IsDefined, IsInt, Min } from 'class-validator';
|
||||||
import { ComposeValidators } from './compose.validator';
|
import { CombinePDecorators } from '../util/decorator';
|
||||||
|
|
||||||
export const IsPosInt = ComposeValidators(IsInt(), Min(0), IsDefined());
|
export const IsPosInt = CombinePDecorators(IsInt(), Min(0), IsDefined());
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { IsAlphanumeric, IsNotEmpty, IsString, Length } from 'class-validator';
|
import { IsAlphanumeric, IsNotEmpty, IsString, Length } from 'class-validator';
|
||||||
import { ComposeValidators } from './compose.validator';
|
import { CombinePDecorators } from '../util/decorator';
|
||||||
|
|
||||||
export const IsRoleName = ComposeValidators(
|
export const IsRoleName = CombinePDecorators(
|
||||||
IsNotEmpty(),
|
IsNotEmpty(),
|
||||||
IsString(),
|
IsString(),
|
||||||
Length(4, 32),
|
Length(4, 32),
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ import {
|
|||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsString
|
IsString
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { ComposeValidators } from './compose.validator';
|
import { CombinePDecorators } from '../util/decorator';
|
||||||
|
|
||||||
export const IsStringList = ComposeValidators(
|
export const IsStringList = CombinePDecorators(
|
||||||
IsArray(),
|
IsArray(),
|
||||||
IsString({ each: true }),
|
IsString({ each: true }),
|
||||||
IsNotEmpty({ each: true }),
|
IsNotEmpty({ each: true }),
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
import { IsAlphanumeric, IsNotEmpty, IsString, Length } from 'class-validator';
|
import { IsAlphanumeric, IsNotEmpty, IsString, Length } from 'class-validator';
|
||||||
import { ComposeValidators } from './compose.validator';
|
import { CombinePDecorators } from '../util/decorator';
|
||||||
|
|
||||||
// Match this with user validators in frontend
|
// Match this with user validators in frontend
|
||||||
// (Frontend is not security focused, but it tells the user what is wrong)
|
// (Frontend is not security focused, but it tells the user what is wrong)
|
||||||
|
|
||||||
export const IsUsername = ComposeValidators(
|
export const IsUsername = CombinePDecorators(
|
||||||
IsNotEmpty(),
|
IsNotEmpty(),
|
||||||
IsString(),
|
IsString(),
|
||||||
Length(4, 32),
|
Length(4, 32),
|
||||||
IsAlphanumeric(),
|
IsAlphanumeric(),
|
||||||
);
|
);
|
||||||
|
|
||||||
export const IsPlainTextPwd = ComposeValidators(
|
export const IsPlainTextPwd = CombinePDecorators(
|
||||||
IsNotEmpty(),
|
IsNotEmpty(),
|
||||||
IsString(),
|
IsString(),
|
||||||
Length(4, 1024),
|
Length(4, 1024),
|
||||||
|
|||||||
Reference in New Issue
Block a user