Loading…
Loading…
Modify and preview validation rules for API operation contracts. Override constraints like patterns, lengths, and numeric ranges for request and response
The Contract Validation Override service provides methods for managing the validation rules applied to the request and response schemas of an API operation. It allows an API publisher to inspect, preview, and permanently modify field-level constraints such as regular expressions, string lengths, and numeric ranges.
This service solves the problem of needing to enforce validation rules that are stricter or different from those originally defined in an operation's base contract (e.g., from an imported WSDL). It operates on the internal contract representation and allows for granular, field-by-field adjustments.
The service has three primary functions:
describe): Fetches and presents the current validation rules for all fields in an operation's request and response schemas.applyPreviewOverrides): Simulates the effect of applying one or more rule overrides to a schema without persisting the changes. This is useful for UI previews.updateFieldRule): Persistently applies a single validation rule override to a field in an operation's contract, regenerates the active configuration, and updates the contract's checksum.The service modifies a structured JSON representation of the API contract. Changes are applied by traversing the schema tree to locate a specific field and then updating its constraint properties.
When describe is called with an operationId, it performs the following steps:
OperationContractRecord for the specified operation.ContractExampleService to generate a descriptive list of all fields for both the request and response schemas.operation_mapping table. These regexes were created by a different mechanism but are still active.a.b.c, a/b/c, and a.b[0].c are all converted to a comparable segment list.mappingRegexOrigin of operation-mapping.The updateFieldRule method applies a permanent change to an operation's contract within a database transaction.
flowchart TD
subgraph updateFieldRule Process
A[API Publisher Call] --> B(Validate Inputs);
B --> C{Input Valid?};
C -- No --> D[Return Error];
C -- Yes --> E[Fetch Operation Contract];
E --> F[Deep-copy Schemas];
F --> G[Find Target Field by Path];
G --> H{Field Found?};
H -- No --> D;
H -- Yes --> I[Apply New Constraints];
I --> J[Recalculate Schema Checksum];
J --> K[Persist Updated Schemas];
K --> L[Regenerate Active Mapping];
L --> M[Describe Updated Contract];
M --> N[Return New Contract View];
endThe process is as follows:
side (request or response), a non-blank fieldPath, and logically consistent constraint values (e.g., minLength cannot be greater than maxLength).fieldPath. The path is a dot-separated string (parent.child) that can include array accessors (array[0].field).null or empty value for a constraint will remove it from the field.pattern (regex) is overridden for the first time, the original pattern from the base contract is preserved in a new baselinePattern property. This ensures the original WSDL-defined regex is not lost.contractChecksum field, calculates a new SHA-256 checksum of the entire schema object, and adds it back as the contractChecksum property. This checksum tracks changes to the contract structure and rules.describe logic to return a fresh view of the newly modified contract.The applyPreviewOverrides method allows for a non-persistent "dry run" of changes. It accepts the current schemas and a list of FieldRuleOverride objects. It performs the same deep-copy, field-finding, and constraint application logic as updateFieldRule for each override, including the checksum refresh. However, it does not persist any changes to the database. It returns the modified schema objects, which can be used to show a user what effect their proposed changes would have.
The service exposes the following public methods for managing contract validation overrides.
describeFetches a detailed description of an operation's contract, including all fields and their validation rules.
Map<String, Object> describe(UUID operationId)
Parameters
| Name | Type | Description |
|---|---|---|
operationId | UUID | The unique identifier for the operation. |
Returns
A Map containing the operation's metadata and annotated lists of requestFields and responseFields.
updateFieldRuleApplies a persistent validation rule change to a single field in an operation's contract.
Map<String, Object> updateFieldRule(UUID operationId, String side, ...)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
operationId | UUID | Yes | The unique identifier for the operation to update. |
side | String | Yes | The schema to modify. Must be request or response. |
fieldPath | String | Yes | The dot-separated path to the field to modify (e.g., userInfo.address.postalCode). |
enumValues | List<String> | No | A list of allowed string values. An empty or null list removes the constraint. |
pattern | String | No | A regular expression the field value must match. A blank or null string removes the constraint. |
length | Integer | No | The exact required length for a string field. null removes the constraint. |
minLength | Integer | No | The minimum required length for a string field. null removes the constraint. |
maxLength | Integer | No | The maximum allowed length for a string field. null removes the constraint. |
minInclusive | String | No | The minimum allowed value (inclusive) for a numeric field, as a string. null removes the constraint. |
maxInclusive | String | No | The maximum allowed value (inclusive) for a numeric field, as a string. null removes the constraint. |
minExclusive | String | No | The minimum allowed value (exclusive) for a numeric field, as a string. null removes the constraint. |
maxExclusive | String | No | The maximum allowed value (exclusive) for a numeric field, as a string. null removes the constraint. |
totalDigits | Integer | No | The maximum number of digits for a decimal field. null removes the constraint. |
fractionDigits | Integer | No | The maximum number of digits to the right of the decimal point. null removes the constraint. |
actor | String | Yes | The identifier of the user or system performing the update. |
Returns
A Map representing the new state of the contract, identical in structure to the output of the describe method.
applyPreviewOverridesApplies a list of overrides to the provided schemas in-memory to generate a preview of the changes.
SchemaOverrideResult applyPreviewOverrides(Map<String, Object> requestSchema, Map<String, Object> responseSchema, List<FieldRuleOverride> overrides)
Parameters
| Name | Type | Description |
|---|---|---|
requestSchema | Map<String, Object> | The current request schema object. |
responseSchema | Map<String, Object> | The current response schema object. |
overrides | List<FieldRuleOverride> | A list of FieldRuleOverride objects defining the changes to preview. |
Returns
A SchemaOverrideResult object containing the modified requestSchema and responseSchema maps.
The FieldRuleOverride object is used to specify a set of validation constraints for a single field. It is used by the applyPreviewOverrides method.
public record FieldRuleOverride(
String side,
String fieldPath,
List<String> enumValues,
String pattern,
Integer length,
Integer minLength,
Integer maxLength,
String minInclusive,
String maxInclusive,
String minExclusive,
String maxExclusive,
Integer totalDigits,
Integer fractionDigits
) {}
| Field | Type | Description |
|---|---|---|
side | String | The schema to modify. Must be request or response. |
fieldPath | String | The dot-separated path to the field. |
enumValues | List<String> | A list of allowed string values. |
pattern | String | A regular expression the field value must match. |
length | Integer | The exact required length for a string field. |
minLength | Integer | The minimum required length for a string field. |
maxLength | Integer | The maximum allowed length for a string field. |
minInclusive | String | The minimum allowed value (inclusive) for a numeric field, as a string. |
maxInclusive | String | The maximum allowed value (inclusive) for a numeric field, as a string. |
minExclusive | String | The minimum allowed value (exclusive) for a numeric field, as a string. |
maxExclusive | String | The maximum allowed value (exclusive) for a numeric field, as a string. |
totalDigits | Integer | The maximum number of digits for a decimal field. |
fractionDigits | Integer | The maximum number of digits to the right of the decimal point. |
When calling the service methods, you may encounter the following error conditions, which are reported by throwing an IllegalArgumentException.
| Error Message | Cause |
|---|---|
operation not found | The provided operationId does not correspond to an existing operation. |
fieldPath is required | The fieldPath parameter was null or an empty string. |
side must be request or response | The side parameter was a value other than request or response (case-insensitive). |
fieldPath was not found in the selected contract | The provided fieldPath does not exist in the specified request or response schema. |
length must be >= 0 | A negative value was provided for length. |
minLength must be >= 0 | A negative value was provided for minLength. |
maxLength must be >= 0 | A negative value was provided for maxLength. |
length cannot be smaller than minLength | The length value was less than minLength. |
length cannot be greater than maxLength | The length value was greater than maxLength. |
minLength cannot be greater than maxLength | The minLength value was greater than maxLength. |
totalDigits must be >= 0 | A negative value was provided for totalDigits. |
fractionDigits must be >= 0 | A negative value was provided for fractionDigits. |
fractionDigits cannot be greater than totalDigits | The fractionDigits value was greater than totalDigits. |
numeric bounds must be valid decimal values | A value for min/maxInclusive/Exclusive could not be parsed as a valid decimal number. |
Internal processing failures, such as an inability to serialize or checksum the contract, will result in an IllegalStateException.