Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cast to json schema #683

Closed
KayleMaster opened this issue Apr 28, 2020 · 2 comments
Closed

Cast to json schema #683

KayleMaster opened this issue Apr 28, 2020 · 2 comments

Comments

@KayleMaster
Copy link

Hi,

Is it possible to implement casting a python dict to a json schema?
Like casting '4' to a number field - yeah, it's a string but can be casted without problems.
I know this is not entirely in the scope of jsonschema's purpose, but if you can give some hints on where to start by extending this project in my own fork that'd be great.

Or is it possible to check instead if the data can be casted successfully to the schema's type? Something like 'loose' validation.

@Julian
Copy link
Member

Julian commented Apr 30, 2020

Hi. Not sure specifically what you're referring to (technically speaking, Python doesn't have "casting", but I assume you mean creating new dicts. But not sure what it'd mean to cast one to a schema).

I assume though you may be looking for something like https://github.com/Julian/Seep/ which was an attempt at what it seems you may mean -- an experimental one that I hoped someone else would pick up and run with, but perhaps it gives you the hints you need.

If you're interested in running with it would love to see it, and possibly can even provide some tips (better there than in this repo though).

Thanks!

@Julian Julian closed this as completed Apr 30, 2020
@KayleMaster
Copy link
Author

KayleMaster commented May 5, 2020

@Julian
That projects seems like something that I'm looking for.
I did this for my case, let me know if you think Seep is doing something similar:

def bool_fitter(value):
    if isinstance(value, str):
        if value.lower() in ["true", "1", "yes"]:
            return True
        elif value.lower() in ["false", "0", "no"]:
            return False
        else:
            return None
    elif isinstance(value, float) or isinstance(value, int):
        if value == 0:
            return False
        elif value == 1:
            return True
    else:
        return None
    return None


type_dictionary = {
    "string": str,
    "number": float_fitter,
    "boolean": bool_fitter,
    "integer": int_fitter,
}

And for float fitter you can handle specific formats such as 1.150.000,00 which can't natively be 'cast' using a simple float(number).

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

No branches or pull requests

2 participants