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): validate tag is unused
Co-authored-by: Kenneth Trecy Tobias <19201.tobias.kennethtrecy.c@gmail.com>
- Loading branch information
1 parent
61464b3
commit 22ddd49
Showing
1 changed file
with
48 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,48 @@ | ||
import type { Request } from "!/types/dependent" | ||
import type { ValidationState, ValidationConstraints } from "!/types/validation" | ||
|
||
import Manager from "%/managers/tag" | ||
|
||
/** | ||
* Validator to check if a tag has no other tag activated. | ||
* | ||
* Note: The validator only works for resources with numerical tag 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 hasPosts = await manager.findWithID(Number(modelID), { | ||
"constraints": { | ||
"filter": { | ||
"existence": "exists", | ||
"mustHavePost": true | ||
} | ||
} | ||
}) | ||
|
||
if (hasPosts) return state | ||
|
||
const error = { | ||
"field": constraints.field, | ||
"friendlyName": constraints.friendlyName, | ||
"messageMaker": ( | ||
field: string, | ||
value: string | ||
) => { | ||
const subject = `The tag with an ID of "${value}"` | ||
const predicate = "must be unused by any posts." | ||
|
||
return `${subject} ${predicate}` | ||
} | ||
} | ||
|
||
throw error | ||
} |