Principle:APIJSON APIJSON Demo Field Validation
| Knowledge Sources | |
|---|---|
| Domains | Security, Validation, Data_Integrity |
| Last Updated | 2026-10-14 00:00 GMT |
Overview
Technique for validating structured field values in API requests against predefined regular expression patterns, ensuring data integrity and preventing malformed or malicious input from reaching the database.
Description
Field Validation in APIJSON uses a compile-time registry of regex patterns keyed by field-name aliases (PHONE, EMAIL, IDCARD, etc.). When a request contains a field whose name maps to a registered pattern, the framework automatically validates the field value against the regex before processing the request.
This solves:
- Data integrity: Prevents invalid phone numbers, emails, or ID card numbers from being persisted.
- Injection prevention: Adds a layer of input sanitization beyond SQL escaping.
- Developer convenience: Instead of writing validation logic per endpoint, developers register patterns in a central map and the framework applies them automatically.
The validation is opt-in: only fields whose names (or aliases) appear in the COMPILE_MAP are validated. Other fields pass through without regex checks.
Usage
Use this principle when:
- Accepting user-provided data with known formats (phone numbers, emails, ID cards).
- You want centralized, declarative validation rather than per-field if-checks.
- You need to reject malformed input early in the request pipeline, before SQL generation.
Theoretical Basis
The validation mechanism uses a static map of compiled regex patterns:
COMPILE_MAP: Map<String, Pattern>
Validation Flow:
1. Request field "phone" maps to alias "PHONE" (via field-name configuration)
2. Lookup COMPILE_MAP["PHONE"] → Pattern
3. Pattern.matcher(fieldValue).matches() → boolean
4. If false: throw IllegalArgumentException
5. If true: proceed to SQL generation
Registered patterns (examples):
| Alias | Pattern | Description |
|---|---|---|
| PHONE | 14[01456879]|15[0-35-9]|...)\d{8}$ | Chinese mobile phone |
^\w+([-+.]\\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ |
Standard email | |
| IDCARD | (^\\d{18}$)|(^\\d{17}(\\d|X|x)$) | Chinese ID card |
[1-9][0-9]{4,} |
Tencent QQ number | |
| TEL | \\d{3,4}-\\)?\\d{7,8}$ | Landline telephone |