Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ permissions:

env:
# The canonical conformance release this SDK is vendored from (ADR-0032).
BQ_CONFORMANCE_VERSION: "1.10.0"
BQ_CONFORMANCE_VERSION: "1.11.0"
# sha256 of that release's fetch.sh: the script runs on the runner, so it is pinned rather than
# trusted from a mutable tag. Bump together with BQ_CONFORMANCE_VERSION.
BQ_FETCH_SH_SHA256: 8bb6ba567b288fb0ac71a70fd17041b2503df119e01998d76fdad372570bf849
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
env:
# The pinned conformance release (ADR-0032). Keep identical to ci.yml.
BQ_CONFORMANCE_VERSION: "1.10.0"
BQ_CONFORMANCE_VERSION: "1.11.0"
BQ_FETCH_SH_SHA256: 8bb6ba567b288fb0ac71a70fd17041b2503df119e01998d76fdad372570bf849
steps:
- uses: actions/checkout@v7
Expand Down
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@ The envelope wire format is versioned separately by `meta.schema_version`

## [Unreleased]

## [1.30.0] - 2026-11-22

Implements ADR-0040 (open `lang` tag): the producer schema's closed `lang` enum is replaced by the pattern
`^[a-z][a-z0-9-]*$` on both `meta.lang` and `dead_letter.lang`, so a new SDK (e.g. Rust) can emit its own tag without a
contract change. Nothing on the wire changes and the envelope stays at `schema_version: 1`; consumers were already
tolerant of any `lang` value (decode and re-emit are untouched).

### Added
- `SchemaValidator` supports the Draft-07 `pattern` keyword. A string that does not match is reported as
`<path>: pattern` (`SchemaValidator::REASON_PATTERN_MISMATCH`). The pattern is read with JSON Schema / ECMA-262
semantics (unanchored search; `^`/`$` inside the pattern are the anchors, and `$` does not match before a trailing
newline). A pattern that cannot be compiled is a schema defect and throws a `LogicException` instead of silently
accepting or rejecting every value.

### Changed
- Bundled `message-envelope.schema.json` and the vendored conformance suite are updated to conformance **1.11.0**
(`unknown-lang` now passes the producer schema; new `malformed-lang` case). CI and release pin
`BQ_CONFORMANCE_VERSION` to `1.11.0`.
- `SchemaValidator`: `lang: "rust"` (or any well-formed tag) is now valid; `"Rust 1"` is rejected with reason `pattern`
rather than `not_in_enum`. Code that matched on `REASON_NOT_IN_ENUM` for `meta.lang` / `dead_letter.lang` must expect
`REASON_PATTERN_MISMATCH`.

## [1.29.0] - 2026-11-11

Brings the opt-in payload validator (ADR-0024) to the K-17 Draft-07 subset behaviour pinned by conformance
Expand Down
59 changes: 55 additions & 4 deletions src/Validation/SchemaValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* tolerates the `urn` inbound alias and ignores fields it does not need), this
* answers "does this match the wire contract exactly as a producer must emit
* it?" — required `job` (no `urn` alias), the full required `meta` block,
* UUID-shaped ids, the `lang` enum, `schema_version` const, and the optional
* UUID-shaped ids, the `lang` tag pattern, `schema_version` const, and the optional
* `dead_letter` block when present. It is the in-process echo of the schema diff
* CI runs against the conformance suite, usable by an app at runtime without any
* network access or a JSON-Schema library.
Expand All @@ -26,8 +26,15 @@
*
* Kept to a hand-rolled structural check (GR-7: zero heavy dependencies): it
* enforces the subset of Draft-07 the envelope actually uses — `type`,
* `required`, `enum`, `const`, `minLength`, `minimum`, `format: uuid` — not the
* whole specification.
* `required`, `enum`, `const`, `minLength`, `minimum`, `pattern`,
* `format: uuid` — not the whole specification.
*
* `pattern` is read as a JSON Schema / ECMA-262 regular expression: unanchored
* search semantics, with `^` and `$` inside the pattern marking the anchors
* (`$` matches only at the very end, never before a trailing newline). It is run
* through `preg_match` in Unicode mode; only the plain regular subset the
* envelope schema uses (character classes, quantifiers, anchors) is guaranteed
* to mean the same thing in both dialects.
*/
final class SchemaValidator
{
Expand All @@ -39,6 +46,10 @@ final class SchemaValidator
public const REASON_NOT_IN_ENUM = 'not_in_enum';
public const REASON_WRONG_CONST = 'wrong_const';
public const REASON_BELOW_MINIMUM = 'below_minimum';
public const REASON_PATTERN_MISMATCH = 'pattern';

/** Candidate `preg_match` delimiters; the first one absent from a pattern is used. */
private const PATTERN_DELIMITERS = ['~', '#', '%', '@', '!', '`', ';', ','];

/** RFC 4122 textual UUID, as the schema's `format: uuid` requires. */
private const UUID_PATTERN = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i';
Expand Down Expand Up @@ -191,7 +202,7 @@ private static function matchesType(string $type, mixed $value): bool
}

/**
* `minLength` and `format: uuid` string constraints.
* `minLength`, `pattern` and `format: uuid` string constraints.
*
* @param array<string, mixed> $schema
*/
Expand All @@ -201,13 +212,53 @@ private static function checkString(array $schema, string $value, string $path):
return self::violation($path, self::REASON_EMPTY_STRING);
}

if (isset($schema['pattern']) && is_string($schema['pattern']) && ! self::matchesPattern($schema['pattern'], $value)) {
return self::violation($path, self::REASON_PATTERN_MISMATCH);
}

if (($schema['format'] ?? null) === 'uuid' && preg_match(self::UUID_PATTERN, $value) !== 1) {
return self::violation($path, self::REASON_NOT_UUID);
}

return null;
}

/**
* Whether `$value` matches the ECMA-262 style `$pattern` (search semantics).
*
* The `u` flag makes the pattern operate on code points; `D` keeps `$` from
* matching before a trailing newline, as it does in ECMA-262. A subject that
* is not valid UTF-8 cannot match. A pattern that does not compile is a defect
* in the schema, not in the envelope, so it throws rather than silently
* accepting (or rejecting) every value.
*
* @throws \LogicException when the pattern cannot be compiled
*/
private static function matchesPattern(string $pattern, string $value): bool
{
$delimiter = null;
foreach (self::PATTERN_DELIMITERS as $candidate) {
if (! str_contains($pattern, $candidate)) {
$delimiter = $candidate;
break;
}
}

if ($delimiter === null) {
throw new \LogicException(sprintf('Schema pattern %s cannot be delimited for preg_match.', json_encode($pattern)));
}

$regex = $delimiter . $pattern . $delimiter . 'uD';

// preg_match() warns on a malformed pattern; the empty-subject probe tells
// a compile failure (false) apart from a plain non-match (0).
if (@preg_match($regex, '') === false) {
throw new \LogicException(sprintf('Schema pattern %s is not a valid regular expression.', json_encode($pattern)));
}

return preg_match($regex, $value) === 1;
}

/**
* `required` keys, then recurse into each declared `properties` entry that is
* present. Unknown keys are allowed (`additionalProperties: true`).
Expand Down
4 changes: 2 additions & 2 deletions src/Validation/message-envelope.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
},
"lang": {
"type": "string",
"enum": ["php", "go", "python", "java", "dotnet", "node"],
"pattern": "^[a-z][a-z0-9-]*$",
"description": "Producer language tag."
},
"schema_version": {
Expand Down Expand Up @@ -101,7 +101,7 @@
},
"lang": {
"type": "string",
"enum": ["php", "go", "python", "java", "dotnet", "node"],
"pattern": "^[a-z][a-z0-9-]*$",
"description": "Language of the SDK that dead-lettered the message."
}
}
Expand Down
Loading