Comments (3)
@jinsoo960 Arrays can be validated with minItems/maxItems while Strings can be validated with minLength/maxLength as per the JSON Schema specification. There is an implicit type check (list/tuple
for minItems/maxItems, str
for minLength/maxLength) along with the length validation. Using minItems along with minLength is arguably an illegal operation since they validate different data types.
https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minlength
https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxlength
https://json-schema.org/draft/2020-12/json-schema-validation.html#name-maxitems
https://json-schema.org/draft/2020-12/json-schema-validation.html#name-minitems
from python-fastjsonschema.
Both minItems/maxItems
and minLength/maxLength
are using the same generator function create_variable_with_length
, this generator function defines a variable {variable}_len
to hold the length of the passed variable. This variable is defined in the generated code only if the object is of the appropriate type (str
for minLength/maxLength
, list/tuple
for minItems/maxItems
). However, the generator will always add it to its internal set of tracked variables (self._variables
).
For instance, if the schema is:
{
"minItems": 1,
"minLength": 1
}
Since both minLength
and minItems
are defined, the generator will generate an if
block with {variable}_len
defined for validating minItems
and add {variable_len}
to its internal set of generated variables. When it generates the if
block for validating minLength
, it will not define {variable}_len
since it has previously added it to its set of tracked variables.
The code block generated would be roughly of the form:
{variable}_is_list = isinstance({variable}, (list, tuple))
if {variable}_is_list:
{variable}_len = len({variable})
if {variable}_len < {minItems}:
raise JSONSchemaException(msg)
if isinstance({variable}, str):
if {variable}_len < {minLength}:
raise JSONSchemaException(msg)
Note that {variable}_len
is not defined in the second if
block.
If the passed object to validate is a string and not a list, {variable}_len
would not be defined in the second if
block as {variable}_is_list
would be False
and the contents of the first if
block would not be evaluated.
My proposed fix is to add a separate generator function create_variable_with_items
which creates a variable {variable}_items
to hold the length of the variable and call this function from generate_min_items/generate_max_items
instead thus resolving the variable name conflict described above. Now, minItems will effectively be ignored if the passed object is not a list
or tuple
while minLength will effectively be ignored if the passed object is not a str
.
This is consistent with the behaviour of the online JSON Schema validator Hyperjump.
from python-fastjsonschema.
@horejsek - I have raised a PR for this.
from python-fastjsonschema.
Related Issues (20)
- Is there a way to support multiple errors instead of showing just the first error in the schema? HOT 1
- Please make a new release HOT 1
- Uniqueness enforced if uniqueItems explicitly set to False HOT 1
- 2.16.1: test_compile_to_code_custom_format fails HOT 3
- 2.16.2 not available on pypi.org HOT 1
- RFC 3339 date month and mday should be two digit HOT 2
- are the compiled validation functions cached? HOT 2
- Datadog's Guarddog detects exec in setup.py and complains HOT 4
- Python 3.10 venv build is failing HOT 1
- sdist is missing tests HOT 1
- Cannot parse int64 types HOT 1
- [Potential bug] not working validation for properties not defined in the schema object HOT 1
- Release request HOT 1
- Wrong tag name for 2.17.0 HOT 1
- 2.17.0 sdist contains "stray" tests/examples/lengths that breaks tests HOT 2
- errors for missing required properties
- Error occurs when fastjsonschema generates empty except block HOT 2
- Feature request: automatically add nested defaults HOT 7
- RFE: is it possible to start making github releases?🤔 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-fastjsonschema.