Giter VIP home page Giter VIP logo

Comments (3)

melroy-tellis avatar melroy-tellis commented on July 20, 2024

@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.

melroy-tellis avatar melroy-tellis commented on July 20, 2024

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.

melroy-tellis avatar melroy-tellis commented on July 20, 2024

@horejsek - I have raised a PR for this.

from python-fastjsonschema.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.