change validation

This commit is contained in:
rubikscraft
2022-03-23 11:17:41 +01:00
parent 0b65d496c3
commit 036f4d4e4d
17 changed files with 178 additions and 119 deletions

View File

@@ -4,13 +4,13 @@ import {
IsEnum, IsString,
ValidateNested
} from 'class-validator';
import { EUser, SimpleUser } from '../../entities/user.entity';
import { EUser, NamePassUser } from '../../entities/user.entity';
import { Permissions, PermissionsList } from '../permissions';
// Api
// UserLogin
export class UserLoginRequest extends SimpleUser {}
export class UserLoginRequest extends NamePassUser {}
export class UserLoginResponse {
@IsString()
@@ -19,7 +19,7 @@ export class UserLoginResponse {
}
// UserRegister
export class UserRegisterRequest extends SimpleUser {}
export class UserRegisterRequest extends NamePassUser {}
export class UserRegisterResponse extends EUser {}

View File

@@ -2,11 +2,12 @@ import { Type } from 'class-transformer';
import {
IsArray,
IsDefined,
IsInt, IsString,
IsInt,
IsString,
Min,
ValidateNested
} from 'class-validator';
import { EUser, SimpleUser, SimpleUsername } from '../../entities/user.entity';
import { EUser, NamePassUser, UsernameUser } from '../../entities/user.entity';
import { Roles } from '../roles.dto';
// UserList
@@ -41,19 +42,19 @@ export class UserListResponse {
}
// UserCreate
export class UserCreateRequest extends SimpleUser {}
export class UserCreateRequest extends NamePassUser {}
export class UserCreateResponse extends EUser {}
// UserDelete
export class UserDeleteRequest extends SimpleUsername {}
export class UserDeleteRequest extends UsernameUser {}
export class UserDeleteResponse extends EUser {}
// UserInfo
export class UserInfoRequest extends SimpleUsername {}
export class UserInfoRequest extends UsernameUser {}
export class UserInfoResponse extends EUser {}
// UserUpdateRoles
export class UserUpdateRolesRequest extends SimpleUsername {
export class UserUpdateRolesRequest extends UsernameUser {
@IsArray()
@IsDefined()
@IsString({ each: true })

View File

@@ -0,0 +1,6 @@
import { EntityID } from '../validators/entity-id.validator';
export class EntityIDObject {
@EntityID()
id: number;
}

View File

@@ -1,12 +1,12 @@
import { Type } from 'class-transformer';
import { IsDefined, IsInt, IsOptional, ValidateNested } from 'class-validator';
import { RoledUser } from '../entities/user.entity';
import { NameRolesUser } from '../entities/user.entity';
export class JwtDataDto {
@IsDefined()
@ValidateNested()
@Type(() => RoledUser)
user: RoledUser;
@Type(() => NameRolesUser)
user: NameRolesUser;
@IsOptional()
@IsInt()

View File

@@ -1,10 +1,10 @@
import { Exclude } from 'class-transformer';
import { IsDefined, IsEnum, IsHash, IsInt, IsOptional } from 'class-validator';
import { IsDefined, IsEnum, IsHash, IsOptional } from 'class-validator';
import { SupportedMime, SupportedMimes } from '../dto/mimes.dto';
import { EntityID } from '../validators/entity-id.validator';
export class EImage {
@IsOptional()
@IsInt()
@EntityID()
id?: number;
@IsHash('sha256')

View File

@@ -1,9 +1,9 @@
import { IsArray, IsEnum, IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { IsArray, IsEnum, IsNotEmpty, IsString } from 'class-validator';
import { Permissions, PermissionsList } from '../dto/permissions';
import { EntityID } from '../validators/entity-id.validator';
export class ERole {
@IsOptional()
@IsInt()
@EntityID()
id?: number;
@IsNotEmpty()

View File

@@ -1,9 +1,9 @@
import { IsEnum, IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { IsEnum, IsNotEmpty, IsString } from 'class-validator';
import { SysPreferences } from '../dto/syspreferences.dto';
import { EntityID } from '../validators/entity-id.validator';
export class ESysPreference {
@IsOptional()
@IsInt()
@EntityID()
id?: number;
@IsNotEmpty()

View File

@@ -1,45 +1,33 @@
import { Exclude } from 'class-transformer';
import {
IsAlphanumeric,
IsArray,
IsInt,
IsNotEmpty,
IsOptional,
IsString,
Length
IsArray, IsOptional,
IsString
} from 'class-validator';
import { Roles } from '../dto/roles.dto';
import { EntityID } from '../validators/entity-id.validator';
import { IsPlainTextPwd, IsUsername } from '../validators/user.validators';
// Match this with user validators in frontend
// (Not security focused, but it tells the user what is wrong)
export class SimpleUsername {
@IsNotEmpty()
@IsString()
@Length(4, 32)
@IsAlphanumeric()
export class UsernameUser {
@IsUsername()
username: string;
}
// This is a simple user object with just the username and unhashed password
export class SimpleUser extends SimpleUsername {
@IsNotEmpty()
@IsString()
@Length(4, 1024)
export class NamePassUser extends UsernameUser {
@IsPlainTextPwd()
password: string;
}
// Add a user object with just the username and roles for jwt
export class RoledUser extends SimpleUsername {
export class NameRolesUser extends UsernameUser {
@IsArray()
@IsString({ each: true })
roles: Roles;
}
// Actual entity that goes in the db
export class EUser extends RoledUser {
@IsOptional()
@IsInt()
export class EUser extends NameRolesUser {
@EntityID()
id?: number;
@IsOptional()

View File

@@ -0,0 +1,10 @@
export const ComposeValidators = (...validators: PropertyDecorator[]): () => PropertyDecorator => {
return () => {
const decorators = [...validators];
return (target: Object, propertyKey: string | symbol): void => {
decorators.forEach((decorator) => decorator(target, propertyKey));
};
}
};

View File

@@ -0,0 +1,5 @@
import { IsInt, IsNotEmpty, IsOptional, Min } from 'class-validator';
import { ComposeValidators } from './compose.validator';
export const EntityID = ComposeValidators(IsOptional(), IsInt(), Min(0));
export const EntityIDRequired = ComposeValidators(IsNotEmpty(), IsInt(), Min(0));

View File

@@ -0,0 +1,18 @@
import { IsAlphanumeric, IsNotEmpty, IsString, Length } from 'class-validator';
import { ComposeValidators } from './compose.validator';
// Match this with user validators in frontend
// (Frontend is not security focused, but it tells the user what is wrong)
export const IsUsername = ComposeValidators(
IsNotEmpty(),
IsString(),
Length(4, 32),
IsAlphanumeric(),
);
export const IsPlainTextPwd = ComposeValidators(
IsNotEmpty(),
IsString(),
Length(4, 1024),
);