← 返回首页
Use environment variables to use std::regex by hkxs · Pull Request #222 · python-rapidjson/python-rapidjson · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Use environment variables to use std::regex#222

Open
hkxs wants to merge 7 commits into
python-rapidjson:masterfrom
hkxs:feature/schema_use_stdregex
Open

Use environment variables to use std::regex#222
hkxs wants to merge 7 commits into
python-rapidjson:masterfrom
hkxs:feature/schema_use_stdregex

Conversation

hkxs commented Dec 30, 2024

Copy link
Copy Markdown

RapidJson uses an in-house regex engine that has limited syntax, we can use std::regex by defining

  • RAPIDJSON_SCHEMA_USE_INTERNALREGEX=0
  • RAPIDJSON_SCHEMA_USE_STDREGEX=1

This patch extends that functionality to python-rapidjson by using the environmental variable RAPIDJSON_SCHEMA_USE_STDREGEX

For example, the following schema uses negative-lookahead (not supported by default engine):

{ "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "some_id", "type": "object", "additionalProperties": False, "required": ["metadata"], "patternProperties": { "^(?!metadata$).+": { "type": "string" } }, "properties": { "metadata": { "type": "integer" } } }

Using this schema leads to a ValidationError using this json {"a": "a", "metadata": 1}:

>>> import rapidjson >>> schema_dict = { "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "some_id", "type": "object", "additionalProperties": False, "required": ["metadata"], "patternProperties": { "^(?!metadata$).+": { "type": "string" } }, "properties": { "metadata": { "type": "integer" } } } >>> validator = rapidjson.Validator(rapidjson.dumps(schema_dict)) >>> validator(rapidjson.dumps({"a": "a", "metadata": 1})) Traceback (most recent call last): File "<python-input-2>", line 1, in <module> validator(rapidjson.dumps({"a": "a", "metadata": 1})) ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rapidjson.ValidationError: ('additionalProperties', '#', '#/a')

Expected behavior:

>>> import rapidjson >>> schema_dict = { "$schema": "https://json-schema.org/draft/2020-12/schema", "$id": "some_id", "type": "object", "additionalProperties": False, "required": ["metadata"], "patternProperties": { "^(?!metadata$).+": { "type": "string" } }, "properties": { "metadata": { "type": "integer" } } } >>> validator = rapidjson.Validator(rapidjson.dumps(schema_dict)) >>> validator(rapidjson.dumps({"a": "a", "metadata": 1})) # no error raised by the validator

lelit commented Dec 31, 2024

Copy link
Copy Markdown
Contributor

Thanks for the improvement.

Ideally we should have a test case for the functionality, even just what you wrote above, but at a minimum I'd say we should expose the availability of StdRegex on the module, say RAPIDJSON_SCHEMA_USES_STDREGEX or something like that, similar to the RAPIDJSON_VERSION attribute.

What do you think?

hkxs commented Jan 2, 2025

Copy link
Copy Markdown
Author

Thanks for the improvement.

Ideally we should have a test case for the functionality, even just what you wrote above, but at a minimum I'd say we should expose the availability of StdRegex on the module, say RAPIDJSON_SCHEMA_USES_STDREGEX or something like that, similar to the RAPIDJSON_VERSION attribute.

What do you think?

You're right it is good to have it exposed on the module, I added it (but I'm not sure if I should add something on typings/rapidjson/init.pyi, let me know if I'm missing something there). I also added an small test case to validate that we can use other regex when the variable is set

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Footer

© 2026 GitHub, Inc.