Principle:APIJSON APIJSON Request Validation
| Knowledge Sources | |
|---|---|
| Domains | Security, Validation, API_Design |
| Last Updated | 2026-10-11 00:00 GMT |
Overview
A declarative validation pattern that checks request content against configurable rules before processing, ensuring required fields are present, forbidden fields are absent, types are correct, and values satisfy constraints.
Description
Request Validation is the process of verifying that an incoming request meets structural and semantic requirements before it is processed. In APIJSON, validation rules are stored in a database table (Request) and keyed by method tag and version, allowing them to be changed at runtime without code changes.
The validation system supports these operation types (defined in the Operation enum):
- MUST: Specifies required fields — all listed fields must be present in the request
- REFUSE: Specifies forbidden fields — none of the listed fields may be present
- TYPE: Type checking — each field must match its declared type (BOOLEAN, NUMBER, STRING, URL, DATE, ARRAY, etc.)
- VERIFY: Constraint checking — regex patterns, range checks, length limits using COMPILE_MAP aliases
- UNIQUE: Uniqueness — the field value must not already exist in the database
- EXIST: Existence — the field value must already exist in the database
- INSERT/UPDATE/REPLACE/REMOVE: Data manipulation rules for auto-setting values
The problem solved is ensuring data integrity and security at the API boundary. Without validation, clients could submit malformed, oversized, or malicious data that corrupts the database or causes runtime errors.
Usage
Use this principle when:
- An API needs configurable validation rules (not hardcoded in business logic)
- Different operations on the same table need different validation (POST vs PUT)
- Validation rules need to evolve without redeployment
Theoretical Basis
# Abstract algorithm (NOT real implementation):
def validate_request(method, tag, request, config_map):
config = config_map.get(f"{method} {tag}")
structure = config["structure"]
# MUST: check required fields
required = structure.get("MUST", "").split(",")
for field in required:
if field not in request:
raise ValidationError(f"Missing required field: {field}")
# REFUSE: check forbidden fields
forbidden = structure.get("REFUSE", "").split(",")
for field in forbidden:
if field in request:
raise ValidationError(f"Forbidden field: {field}")
# TYPE: check field types
type_rules = structure.get("TYPE", {})
for field, expected_type in type_rules.items():
if field in request:
if not check_type(request[field], expected_type):
raise ValidationError(f"Type mismatch for {field}")
# VERIFY: check value constraints
verify_rules = structure.get("VERIFY", {})
for key, constraint in verify_rules.items():
field, operator = parse_key_suffix(key)
if field in request:
if not check_constraint(request[field], constraint, operator):
raise ValidationError(f"Constraint failed for {field}")
return request