Skip to main content

Provides random samples of given json schema

Project description

freddy

Provides randomized json data (samples) that complies with a given schema.

Works both for json schema and pydantic models.

Usage

pydantic

from pprint import pprint
from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel
import freddy


class User(BaseModel):
    id: int
    name = 'John Doe'
    signup_ts: Optional[datetime] = None
    friends: List[int] = []


sample = freddy.sample(User)
pprint(sample)
{'friends': [717, 235, 439, 589], 'id': 565, 'signup_ts': '1907-06-22T18:01:00'}
User.validate(sample)
User(id=565, signup_ts=datetime.datetime(1907, 6, 22, 18, 1), friends=[717, 235, 439, 589], name='John Doe')

jsonschema

from pprint import pprint
import jsonschema
import freddy

family_schema = {
    "type": "array",
    "items": {
        "properties": {
            "member": {"$ref": "#/definitions/person"},
            "role": {"$ref": "#/definitions/role"},
        },
        "type": "object",
    },
    "maxItems": 5,
    "minItems": 1,
    "definitions": {
        "person": {
            "properties": {
                "age": {"type": "integer"},
                "name": {"type": "string"},
                "pets": {
                    "items": {"$ref": "#/definitions/pet"},
                    "maxItems": 2,
                    "type": "array",
                },
            },
            "type": "object",
        },
        "pet": {
            "properties": {
                "kind": {"enum": ["dog", "cat"], "type": "string"},
                "name": {"type": "string"},
            },
            "type": "object",
        },
        "role": {
            "enum": [
                "father",
                "mather",
                "son",
                "daughter",
                "aunt",
                "grandma",
                "grandpa",
            ],
            "type": "string",
        },
    }
}

# Get 10 random samples
for i in range(10):
    sample_family = freddy.sample(family_schema)

    # Validate against schema
    jsonschema.validate(sample_family, family_schema)

pprint(sample_family)
[
    {"member": {"age": 77, "name": "k", "pets": []}, "role": "grandma"},
    {"member": {"age": 64, "name": "naifvxf", "pets": []}, "role": "grandpa"},
    {
        "member": {
            "age": 23,
            "name": "itruydotrj",
            "pets": [{"kind": "cat", "name": "o"}, {"kind": "cat", "name": "uonmvfgd"}],
        },
        "role": "son",
    },
]

Install

pip install freddy

Development

# Clone the repo
git@github.com:lferran/freddy.git
cd freddy

make develop

# Run tests
make tests

JSON Schema support

Conforms to JSON Schema Draft 7. The following features are supported:

  • boolean type

  • null type

  • string type

  • number type

  • integer type

  • array type

  • object type

  • definitions/references

  • Boolean type

  • consts

  • exclusiveMinimum and exclusiveMaximum in integers and numbers.

  • number multipleOf keyword

  • required keyword

  • additionalProperties

  • string pattern regex keyword

  • all string built-in formats

  • be able to provide custom basic type factories

  • multiple types: {"type": ["string", "array"]}

  • look into allOf: generate multiple objects + merge

Does not support:

  • ID referencing
  • allOf and not keywords
  • conditional keywords if, then and else
  • patternProperties on objects
  • property and schema dependencies on objects.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

freddy-3.0.1.tar.gz (6.8 kB view details)

Uploaded Source

File details

Details for the file freddy-3.0.1.tar.gz.

File metadata

  • Download URL: freddy-3.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

File hashes

Hashes for freddy-3.0.1.tar.gz
Algorithm Hash digest
SHA256 d46b8cd863b457f30af2aad27f1e15fd485c7bf84a9882e8bbf318b4e8319d5a
MD5 81a47d5dd4ef8c41227c9bdb1190ee55
BLAKE2b-256 11b94a7deebbd023e438750953c55229aab6d9b290159fb1b5752f8f47f17ad5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page