jsf – Faking JSON data from a schema

https://github.com/ghandic/jsf

Use jsf along with fake data generators to provide consistent and meaningful fake data for your system.

Main Features

Provides out of the box data generation from any JSON schema ?
Extendable custom data pr…


This content originally appeared on DEV Community and was authored by Andy Challis

https://github.com/ghandic/jsf

Use jsf along with fake data generators to provide consistent and meaningful fake data for your system.

Main Features

  • Provides out of the box data generation from any JSON schema ?
  • Extendable custom data providers using any lambda functions ?
  • Multi level state for dependant data (eg multiple objects sharing value, such as children with same surname) ?
  • Inbuilt validation of fake JSON produced ✅
  • In memory conversion from JSON Schema to Pydantic Models with generated examples ?
  • Seamless integration with FastAPI ?

Installation

$ pip install jsf

---> 100%

Usage

Basic ?

from jsf import JSF

faker = JSF(
    {
        "type": "object",
        "properties": {
            "name": {"type": "string", "$provider": "faker.name"},
            "email": {"type": "string", "$provider": "faker.email"},
        },
        "required": ["name", "email"],
    }
)

fake_json = faker.generate()

Results in ...

{
    'name': 'Jesse Phillips', 
    'email': 'xroberson@hotmail.com'
}

From JSON file ?

from jsf import JSF

faker = JSF.from_json("demo-schema.json")
fake_json = faker.generate()

Or run stright from the commandline...

Native install

jsf --schema src/tests/data/custom.json --instance wow.json

Docker

docker run -v $PWD:/data challisa/jsf jsf --schema /data/custom.json --instance /data/example.json

FastAPI Integration ?

Create a file main.py with:

from jsf import JSF
from fastapi import FastAPI

app = FastAPI(docs_url="/")
generator = JSF.from_json("custom.json")


@app.get("/generate", response_model=generator.pydantic())
def read_root():
    return generator.generate()

Run the server with:

$ uvicorn main:app --reload

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [28720]
INFO:     Started server process [28722]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

Navigate to http://127.0.0.1:8000 and check out your endpoint. Notice the following are all automatically created:

  • Schema with descriptions and examples
  • Example response
  • Data generation by clicking "try it out"

Example Swagger UI - Page 1
Example Swagger UI - Page 2
Example Swagger UI - Page 3
Example Swagger UI - Page 4


This content originally appeared on DEV Community and was authored by Andy Challis


Print Share Comment Cite Upload Translate Updates
APA

Andy Challis | Sciencx (2021-05-04T04:47:08+00:00) jsf – Faking JSON data from a schema. Retrieved from https://www.scien.cx/2021/05/04/jsf-faking-json-data-from-a-schema/

MLA
" » jsf – Faking JSON data from a schema." Andy Challis | Sciencx - Tuesday May 4, 2021, https://www.scien.cx/2021/05/04/jsf-faking-json-data-from-a-schema/
HARVARD
Andy Challis | Sciencx Tuesday May 4, 2021 » jsf – Faking JSON data from a schema., viewed ,<https://www.scien.cx/2021/05/04/jsf-faking-json-data-from-a-schema/>
VANCOUVER
Andy Challis | Sciencx - » jsf – Faking JSON data from a schema. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/04/jsf-faking-json-data-from-a-schema/
CHICAGO
" » jsf – Faking JSON data from a schema." Andy Challis | Sciencx - Accessed . https://www.scien.cx/2021/05/04/jsf-faking-json-data-from-a-schema/
IEEE
" » jsf – Faking JSON data from a schema." Andy Challis | Sciencx [Online]. Available: https://www.scien.cx/2021/05/04/jsf-faking-json-data-from-a-schema/. [Accessed: ]
rf:citation
» jsf – Faking JSON data from a schema | Andy Challis | Sciencx | https://www.scien.cx/2021/05/04/jsf-faking-json-data-from-a-schema/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.