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

Commit

Permalink
intrn(validator): validate tag is unused
Browse files Browse the repository at this point in the history
Co-authored-by: Kenneth Trecy Tobias <19201.tobias.kennethtrecy.c@gmail.com>
  • Loading branch information
lemredd and KennethTrecy committed Nov 6, 2022
1 parent 61464b3 commit 22ddd49
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions server/validators/manager/has_no_other_posts.ts
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
}

0 comments on commit 22ddd49

Please sign in to comment.