This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
generated from KennethTrecy/web_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
intrn(validator): make validator to check if role is only one related
- Loading branch information
1 parent
c698af4
commit e618389
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |