freddy
Provides randomized json data (samples) that complies with a given schema.
Works both for json schema and pydantic models.
Usage
pydantic
import datetime
from pprint import pprint
from typing import List, Optional
from pydantic import BaseModel, Field
import freddy
class User(BaseModel):
id: int
name = 'John Doe'
signup_ts: Optional[datetime.datetime] = None
friends: List[int] = []
pattern_field: str = Field(..., regex=r"^[-_a-zA-Z0-9]+$")
sample = freddy.sample(User)
pprint(sample)
{'id': 452, 'signup_ts': '1903-03-12T20:20:00', 'friends': [675, 408], 'pattern_field': 'EUvKs7BIK-Ne', 'name': 'xfphlync'}
User.validate(sample)
User(id=452, signup_ts=datetime.datetime(1903, 3, 12, 20, 20), friends=[675, 408], pattern_field='EUvKs7BIK-Ne', name='xfphlync')
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
-
exclusiveMinimumandexclusiveMaximumin integers and numbers. -
number
multipleOfkeyword -
string
patternregex keyword -
requiredkeyword -
additionalProperties -
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
allOfandnotkeywords- conditional keywords
if,thenandelse patternPropertieson objects- property and schema
dependencieson objects.
Metadata
Release files for freddy 3.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| freddy-3.1.0.tar.gz | 6.9 kB | Without provenance | Details for freddy-3.1.0.tar.gz |
Release files / freddy-3.1.0.tar.gz
| Download URL | freddy-3.1.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
37f6e075efede218a6b1870589d39eadb930a88fc908e3ecbdd47be73b0f7eb5
|
|
BLAKE2b-256 checksum How to use checksums |
831efe1299025001761a1cd0c29ed8fff15a4ca98f332ee6fa8fc372a4dfb9f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.6
|
Security for freddy 3.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.