Skip to content

Allow formats for non-string types #86

Open
@Stranger6667

Description

@Stranger6667

As per Draft 4 and Draft 7 validation docs:

A format attribute can generally only validate a given set of instance types. If the type of the instance to validate is not in this set, validation for this format attribute and instance SHOULD succeed.

Newer drafts are more clear on this matter. For example, Draft 2020-12 additionally provides this:

All format attributes defined in this section apply to strings, but a format attribute can be specified to apply to any instance types defined in the data model defined in the core JSON Schema.

Even though, that the supported drafts (4, 6, 7) are a bit vague about instance types, I think that it could be treated as they allow non-string instances. The Open API spec explicitly uses it with int32, float, and other formats that are applicable for non-string types.

jsonschema also supports arbitrary types for its format checkers, at least based on its docstring:

        Check whether the instance conforms to the given format.
        Arguments:
            instance (*any primitive type*, i.e. str, number, bool):
                The instance to check

Currently, hypothesis-jsonschema raises an error if a strategy produces a non-string value.

What do you think about removing this restriction?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions