[REFACTOR](system) Reparent string constraints under PatternConstraint + test improvements#458
Conversation
Victor Schappert (vcschapp)
left a comment
There was a problem hiding this comment.
This is fantastic, especially the:
Net -112 lines.
And also the:
Enables isinstance-based dispatch for pattern constraints in downstream codegen.
Would you mind humoring me with the numpy-style docs? I was so close to getting it all on numpy.
packages/overture-schema-system/src/overture/schema/system/field_constraint/string.py
Outdated
Show resolved
Hide resolved
aa3a6c3 to
2b8a8bd
Compare
|
Now -285 net lines after I found some more things to simplify. I also fixed a JSON Schema validation bug with |
packages/overture-schema-system/src/overture/schema/system/field_constraint/string.py
Outdated
Show resolved
Hide resolved
Eliminates duplicated validate() and __get_pydantic_json_schema__() across CountryCodeAlpha2, HexColor, LanguageTag, NoWhitespace, SnakeCase, PhoneNumber, RegionCode, and WikidataId constraints. Each is now a thin __init__-only wrapper calling super().__init__(). PatternConstraint gains optional keyword-only description, min_length, max_length parameters for JSON Schema annotations. StringConstraint gains _raise_validation_error() to deduplicate error construction across PatternConstraint, JsonPointerConstraint, and StrippedConstraint.
The previous pattern ^(\S.*)?\S$ required at least one non-whitespace
character, rejecting empty string. The validator itself accepts empty
string ("" == "".strip()), so the JSON schema was more restrictive
than the Python validation.
New pattern ^(\S(.*\S)?)?$ matches empty string (outer group optional),
single non-whitespace chars, and strings bookended by non-whitespace.
Updated all JSON schema baselines and inline expectations.
Replace `len(errors()) > 0` with error message assertions in hex color and no-whitespace invalid tests. The weak assertions only checked that validation failed, not that the correct error was raised.
Replace 16 individual valid/invalid test methods with two parametrized tests driven by PATTERN_CONSTRAINT_CASES. Covers all 8 PatternConstraint subclasses: LanguageTag, CountryCodeAlpha2, RegionCode, WikidataId, PhoneNumber, HexColor, NoWhitespace, SnakeCase. Moved SnakeCaseConstraint tests from TestErrorHandling (where they were misplaced) into the parametrized data. Non-PatternConstraint tests remain as standalone methods: base PatternConstraint (custom pattern), StrippedConstraint, and JsonPointerConstraint (empty-string special case).
2b8a8bd to
617f290
Compare
❌ PR Title Validation FailedError: No release type found in pull request title "Reparent string constraints under PatternConstraint + test improvements". Add a prefix to indicate what kind of release this pull request corresponds to. For reference, see https://www.conventionalcommits.org/ Available types:
Examples:
See the Overture Maps Foundation GitHub usage guidelines for full documentation. |
Summary
StringConstraintsubclasses (CountryCodeAlpha2,HexColor,LanguageTag,NoWhitespace,SnakeCase,PhoneNumber,RegionCode,WikidataId) underPatternConstraintas thin__init__-only wrappersPatternConstraintwith optionaldescription,min_length,max_lengthkeyword parameters for JSON Schema annotations_raise_validation_error()helper onStringConstraintto deduplicate error construction acrossPatternConstraint,JsonPointerConstraint, andStrippedConstraintStrippedConstraintJSON schema pattern to accept empty string (matching Python validator behavior)-285 net lines across constraints and tests. Enables
isinstance-based dispatch for pattern constraints in downstream codegen.JsonPointerConstraintandStrippedConstraintremain as directStringConstraintsubclasses (non-regex validation logic).Test plan
isinstanceparametrized test confirms all 8 subclasses arePatternConstraintinstancesPatternConstraintkwargs emit/omit JSON Schema annotations correctlyStrippedConstraintempty-string acceptance tested against both validator and JSON schema patternmake checkpasses