Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
intrn(validator): make validator to check if role is only one related
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Sep 16, 2022
1 parent c698af4 commit e618389
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions server/validators/manager/is_the_only_role_to_any_user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { Request } from "!/types/dependent"
import type { ValidationState, ValidationConstraints } from "!/types/validation"

import Manager from "%/managers/role"

/**
* Validator to check if role is the only role of any user.
*
* Note: The validator only works for resources with numerical role IDs only.
*/
export default async function(
currentState: Promise<ValidationState>,
constraints: ValidationConstraints<Request>
): Promise<ValidationState> {
const state = await currentState

if (state.maySkip) return state

const manager = new Manager(constraints.request)

const modelID = state.value
const isTheOnlyRoleToAnyUser = await manager.isTheOnlyRoleToAnyUser(modelID)

if (isTheOnlyRoleToAnyUser) return state

const error = {
"field": constraints.field,
"friendlyName": constraints.friendlyName,
"messageMaker": (
field: string,
value: string
) => {
const subject = `The "${field}" with a value of "${value}"`
const predicate = "should have a user which is also have other rules after deletion."

return `${subject} ${predicate}`
}
}

throw error
}

0 comments on commit e618389

Please sign in to comment.