A redaction policy written in PhiSQL is a text file that belongs in version control next to the code it protects. Philter and Phileas load policy JSON, not PhiSQL, so something has to compile it. Do that in your build. A misspelled entity type or a broken statement then fails a pull request instead of reaching a running Philter.
This guide sets that up with the PhiSQL reference compilers, which ship for Python, Java, and .NET and produce the same policy for the same input. The JSON is equivalent, but its formatting differs between compilers, so compare policies as JSON (for example with jq -S .), not byte for byte. For the language itself, see Authoring redaction policies with PhiSQL.
Pick a compiler
All three compilers target redaction policy schema 1.4.0 in their current releases:
| Language | Package | Version | Requires |
|---|---|---|---|
| Python | phisql on PyPI | 1.6.0 | Python 3.9 or later |
| Java | ai.philterd:phisql on Maven Central | 1.7.0 | Java 17 or later |
| .NET | Philterd.PhiSql on NuGet | 1.6.0 | .NET 8 or .NET 10 |
Each package bundles the PhiSQL catalog and the policy schema, so it needs no checkout of the PhiSQL repository. Pin the version: a newer compiler can target a newer schema, and the compatibility table lists which schema each release targets. Use whichever language your build already runs. The rest of this guide uses Python for the CI script, then shows the same check in Java and .NET.
Name policy files after their policy
A compiled policy takes its name from the file name. A POLICY statement in the file is optional, but if it is there, it must match the file name, with hyphens and underscores treated alike. These two files are used below.
policies/claims-intake.phisql:
POLICY claims_intake;
REDACT EMAIL_ADDRESS WITH REDACT;
REDACT SSN WITH LAST_4;
REDACT PHONE_NUMBER WITH MASK;
policies/support-tickets.phisql:
POLICY support_tickets;
REDACT EMAIL_ADDRESS, PHONE_NUMBER WITH REDACT;
REDACT SSN WITH LAST_4;
Compile from the command line
The Python package installs a phisql command that compiles one file and writes the policy JSON to standard output:
pip install phisql==1.6.0
phisql policies/support-tickets.phisql
{
"identifiers": {
"emailAddress": {
"emailAddressFilterStrategies": [
{
"strategy": "REDACT"
}
]
},
"phoneNumber": {
"phoneNumberFilterStrategies": [
{
"strategy": "REDACT"
}
]
},
"ssn": {
"ssnFilterStrategies": [
{
"strategy": "LAST_4"
}
]
}
}
}
The exit status says what went wrong, so a script can stop on it:
| Status | Meaning |
|---|---|
0 | Compiled. The JSON is on standard output. |
2 | Parse error: the file is not valid PhiSQL. |
3 | Compile error: the file parses, but names something the catalog does not define, or breaks a rule such as the policy name. |
64 | Usage error, including a file that does not exist. |
1 | Any other error, such as a file that exists but cannot be read. |
Warnings go to standard error as warning: ... lines and do not change the status. Three common mistakes and what they produce:
$ phisql bad/typo.phisql # REDACT SOCIAL_SECURITY WITH REDACT;
compile error: Unknown entity type: SOCIAL_SECURITY
$ echo $?
3
$ phisql bad/broken.phisql # REDACT SSN WITH;
parse error: line 3:15 missing {'REDACT', 'MASK', 'ENCRYPT', 'FPE_ENCRYPT', 'HASH_SHA256', 'RANDOM_REPLACE', 'STATIC_REPLACE', 'LAST_4', 'TRUNCATE_TO_YEAR', 'TRUNCATE', 'SHIFT', 'RELATIVE', 'ABBREVIATE', 'MAP_REPLACE', 'ZERO_LEADING', 'SAFE_HARBOR'} at ';'
$ echo $?
2
$ phisql bad/mismatch.phisql # POLICY other_name; in mismatch.phisql
compile error: POLICY declaration name 'other_name' does not match the expected name 'mismatch'. Either omit the POLICY statement or change it to match.
$ echo $?
3
Check every policy in CI
scripts/compile-policies.sh compiles every file in policies/ into build/policies/, then checks the compiled policies, and any policy JSON you keep by hand in policies/, against the schema bundled with the compiler. It exits non-zero if any file fails either step:
#!/usr/bin/env bash
# Compile every PhiSQL policy into build/policies/, then check the results, and
# any JSON policies kept in policies/, against the schema bundled with the compiler.
set -euo pipefail
mkdir -p build/policies
status=0
for src in policies/*.phisql; do
out="build/policies/$(basename "${src%.phisql}").json"
if phisql "$src" > "$out"; then
echo "compiled $src -> $out"
else
echo "FAILED $src" >&2
rm -f "$out"
status=1
fi
done
python - <<'PY'
import glob, json, sys
from jsonschema import Draft202012Validator
from phisql import PolicySchema
validator = Draft202012Validator(PolicySchema.get_schema_dict())
failed = False
for path in sorted(glob.glob("build/policies/*.json") + glob.glob("policies/*.json")):
errors = list(validator.iter_errors(json.load(open(path))))
for e in errors:
print(f"{path}: {e.message}", file=sys.stderr)
failed |= bool(errors)
print(f"schema {PolicySchema.get_supported_schema_version()}: "
f"{'errors found' if failed else 'all policies valid'}")
sys.exit(1 if failed else 0)
PY
exit $status
It needs jsonschema alongside phisql:
pip install phisql==1.6.0 jsonschema
./scripts/compile-policies.sh
compiled policies/claims-intake.phisql -> build/policies/claims-intake.json
compiled policies/support-tickets.phisql -> build/policies/support-tickets.json
schema 1.4.0: all policies valid
With the misspelled policy added to policies/, the script reports it and exits with status 1:
compiled policies/claims-intake.phisql -> build/policies/claims-intake.json
compiled policies/support-tickets.phisql -> build/policies/support-tickets.json
compile error: Unknown entity type: SOCIAL_SECURITY
FAILED policies/typo.phisql
schema 1.4.0: all policies valid
The compiler already rejects most mistakes, so the schema check matters most for policy JSON written or edited by hand. For a strategy misspelled as REDACTED in such a file, it reports:
'REDACTED' is not one of ['REDACT', 'RANDOM_REPLACE', 'STATIC_REPLACE', 'CRYPTO_REPLACE', 'FPE_ENCRYPT_REPLACE', 'HASH_SHA256_REPLACE', 'LAST_4', 'MASK', 'TRUNCATE', 'ABBREVIATE', 'MAP_REPLACE']
A GitHub Actions job that runs the script on every pull request and keeps the compiled policies as an artifact:
name: policies
on: [push, pull_request]
jobs:
compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install phisql==1.6.0 jsonschema
- run: ./scripts/compile-policies.sh
- uses: actions/upload-artifact@v4
with:
name: policies
path: build/policies/
Load the artifact into Philter with POST /api/policies?name=<name>, or point Phileas at the files.
The same check in Java
In a Maven build, a JUnit test can compile every policy, so mvn test fails on a bad one. Add the compiler as a test dependency:
<dependency>
<groupId>ai.philterd</groupId>
<artifactId>phisql</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
src/test/java/com/example/PolicyCompileTest.java, which compiles each file in src/main/policies/ and writes the JSON to target/policies/:
package com.example;
import ai.philterd.phisql.CompileResult;
import ai.philterd.phisql.Compiler;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
class PolicyCompileTest {
private static final Path SOURCES = Path.of("src/main/policies");
private static final Path OUTPUT = Path.of("target/policies");
// One test per .phisql file. A parse or compile error fails that test, and
// the build with it. Each compiled policy is written to target/policies/.
@TestFactory
Stream<DynamicTest> everyPolicyCompiles() throws Exception {
Files.createDirectories(OUTPUT);
final Compiler compiler = new Compiler();
return Files.list(SOURCES)
.filter(p -> p.toString().endsWith(".phisql"))
.sorted()
.map(source -> DynamicTest.dynamicTest(source.getFileName().toString(), () -> {
final CompileResult result = compiler.compile(source);
Files.writeString(OUTPUT.resolve(result.policyName() + ".json"), result.toJsonString());
}));
}
}
With the two policies above, mvn test reports:
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
With the misspelled policy added, the build fails and names the error:
ai.philterd.phisql.Compiler$CompileException: Unknown entity type: SOCIAL_SECURITY
[ERROR] Tests run: 3, Failures: 0, Errors: 1, Skipped: 0
[INFO] BUILD FAILURE
The JSON in target/policies/ is the same policy the Python compiler produces, with different formatting: Jackson’s pretty printer writes "key" : [ { and no trailing newline.
The same check in .NET
A small console project does the same in a .NET build. Add the package:
dotnet new console -n CompilePolicies -f net8.0
cd CompilePolicies
dotnet add package Philterd.PhiSql --version 1.6.0
Replace Program.cs:
using Philterd.PhiSql;
// Compiles every .phisql file in the given directory into <output>/<name>.json.
// Exits 1 if any file fails to parse or compile, so a CI step fails with it.
var sources = args.Length > 0 ? args[0] : "policies";
var output = args.Length > 1 ? args[1] : "build/policies";
Directory.CreateDirectory(output);
var compiler = new Compiler();
var failed = false;
foreach (var file in Directory.GetFiles(sources, "*.phisql").OrderBy(f => f))
{
try
{
var result = compiler.CompileFile(file);
File.WriteAllText(Path.Combine(output, result.PolicyName + ".json"), result.ToJsonString());
Console.WriteLine($"compiled {file}");
}
catch (Exception e)
{
Console.Error.WriteLine($"FAILED {file}: {e.Message}");
failed = true;
}
}
Console.WriteLine($"schema {PolicySchema.GetSupportedSchemaVersion()}");
return failed ? 1 : 0;
Run it from the repository root:
dotnet run --project CompilePolicies -- policies build/policies
compiled policies/claims-intake.phisql
compiled policies/support-tickets.phisql
schema 1.4.0
With the misspelled policy added, it prints FAILED policies/typo.phisql: Unknown entity type: SOCIAL_SECURITY and exits with status 1.
Where to go next
- Authoring redaction policies with PhiSQL covers the statements themselves.
- Column rules for CSV and Excel in PhiSQL adds rules per column for tabular files.
- The PhiSQL specification is the reference for every statement and catalog entry.
- A policy that compiles is not necessarily a policy that finds what you need. Measure it on your own documents with Philter Scope.