---
title: ContextValidator
summary: API reference for ContextValidator.
status: draft
owner: SAIF Doc Gen
last_reviewed: 2026-09-24
library: SAIF.Services
reference_type: package
---

# ContextValidator

Implements `IContextValidator`, confirming that a policy or agency number resolved from web context actually exists and is usable before downstream document operations proceed. It depends on `IPolicyQueryProvider` to look up the policy, `IPolicySecurityService` to confirm the current user is authorized for that policy, and `IAgencyService` to look up the agency; it throws `InvalidContextException` or `InvalidContextForUserException` rather than returning a result.

## Purpose
- Confirms a policy number identifies a real, non-application-status policy that the current user is authorized to access.
- Confirms an agency number identifies a real agency record.

## Context
- Namespace: `SAIF.Services.OutputDocuments`
- Assembly/package: `SAIF.Services` (packaged inside the `SAIF.SAIFCode.Core` NuGet package)

## Public members
| Member | Signature | Purpose |
| --- | --- | --- |
| ContextValidator(...) | `public ContextValidator(IPolicySecurityService policySecurityService, IAgencyService agencyService, IPolicyQueryProvider policyQueryProvider)` | Wires in the policy security, agency, and policy-query collaborators used by the validation methods. |
| ValidatePolicyNumber | `public void ValidatePolicyNumber(long policyNumber)` | Throws `InvalidContextException` if the policy does not exist, `InvalidContextForUserException` if the current user is not authorized for it, or `InvalidContextException("InvalidPolicyStatus")` if the policy is still in application status; otherwise returns normally. |
| ValidateAgencyNumber | `public void ValidateAgencyNumber(long agencyNumber)` | Throws `InvalidContextException("InvalidAgencyContext")` if no agency exists for the given number; otherwise returns normally. |

## Behavioral notes
- Validation is expressed entirely through thrown exceptions rather than return values, so callers must catch `ContextException`-derived types to react to invalid context (as seen in `ViewDocumentService.GetImageFile`).
- A policy whose `Status.PolicyStatusGroup` is `"APP"` (application/pending) is treated as an invalid context even though the policy record exists.

## Sources
- Services/SAIF.Services/OutputDocuments/ContextValidator.cs
