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

Loading schema from file #177

Closed
BorisWilhelms opened this issue Jun 26, 2023 · 8 comments
Closed

Loading schema from file #177

BorisWilhelms opened this issue Jun 26, 2023 · 8 comments

Comments

@BorisWilhelms
Copy link
Contributor

Hi,

is there any way to load a schema from a file? Tried serializing an existing schema to JSON, but loading (via Object.parse_raw) fails, because there is no type discriminator for the attributes.

Is there already another way to do that? If not, are you planning to implement this or would you accept a PR for this?

Thanks
Boris

@eyurtsev
Copy link
Owner

Hi Boris!

Thanks for checking out the library!

I'm on vacation until the start of August so will be delayed at responding.

One possibility is using kor with pydantic style schema rather than native kor schema and relying on the adapter to map from pydantic schema to kor schema (see validation section in docs). This gives control over serialization and also creates a validator at the same time.

Would this work for your use case?

@BorisWilhelms
Copy link
Contributor Author

Hi Eugene,

thank you for the response while you are on vacation.

I am not that familiar with python and pydantic, so maybe it might be possible to use it for out use case.

Our use case is that the schema will be defined in a different application and can be changed by the user. My understanding with pydantic is, that I need to define the schema/model in code?

Alternatively, with a little change to the ExtractionSchemaNode (BorisWilhelms@e0bfa37) it is possible to load the schema from JSON. The change introduces that the type of the attribute must be defined via the property $type in JSON. Here an example JSON:

{
    "id": "person",
    "description": "Information about a person",
    "many": false,
    "attributes": [
        {
            "$type": "Text",
            "id": "name",
            "description": "The name of the person",
            "many": false,
            "examples": []
        },
        {
            "$type": "Text",
            "id": "position",
            "description": "The position of the person",
            "many": false,
            "examples": []
        },
        {
            "$type": "Number",
            "id": "age",
            "description": "The age of the person",
            "many": false,
            "examples": []
        }
    ],
    "examples": []
}

IMHO this is not the "prettiest" solution, but it works. And as I said, I don't know if our use case can be implemented with pydantic directly, but I will do some more research.

@eyurtsev
Copy link
Owner

That works! Will welcome a PR :). If possible could you add relevant unit tests to verify serialization works?

@BorisWilhelms
Copy link
Contributor Author

Will check what I can do with my simple python skills and create a PR!

Have a nice vacation!

eyurtsev pushed a commit that referenced this issue Jun 27, 2023
This PR makes the schema loadable from JSON via `Object.parse_raw`. 

PR includes feature, tests and documentation

Link to Issue #177
@eyurtsev
Copy link
Owner

Thank you merged

@eyurtsev
Copy link
Owner

released as version 0.11.0

@BorisWilhelms
Copy link
Contributor Author

Hey Eugene,

I managed to make the schema serializable to JSON. Please check PR #184 if it fits for you!

eyurtsev pushed a commit that referenced this issue Jul 3, 2023
This PR makes the schema serializable to JSON via Object.json()

PR includes feature, tests and documentation

Link to Issue #177
@BorisWilhelms
Copy link
Contributor Author

Since this is all merged the issue can be closed.

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