Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lferran/freddy
Provides random fake data valid for a given schema
https://github.com/lferran/freddy
fast-api fastapi json json-api json-schema open-api pydantic python python3 random rest-api restapi swagger
Last synced: about 2 months ago
JSON representation
Provides random fake data valid for a given schema
- Host: GitHub
- URL: https://github.com/lferran/freddy
- Owner: lferran
- License: gpl-3.0
- Created: 2020-04-05T06:40:38.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-30T11:11:16.000Z (over 3 years ago)
- Last Synced: 2024-10-31T07:51:32.597Z (about 2 months ago)
- Topics: fast-api, fastapi, json, json-api, json-schema, open-api, pydantic, python, python3, random, rest-api, restapi, swagger
- Language: Python
- Homepage:
- Size: 80.1 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.rst
- License: LICENSE
Awesome Lists containing this project
README
# freddy
Provides randomized json data (samples) that complies with a given
schema.Works both for json schema and pydantic models.
## Usage
### pydantic
```python
import datetime
from pprint import pprint
from typing import List, Optional
from pydantic import BaseModel, Field
import freddyclass 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
```python
from pprint import pprint
import jsonschema
import freddyfamily_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
``` shell
pip install freddy
```## Development
``` shell
# Clone the repo
[email protected]:lferran/freddy.git
cd freddymake develop
# Run tests
make tests
```## JSON Schema support
Conforms to JSON Schema Draft 7. The following features are supported:
- [x] boolean type
- [x] null type
- [x] string type
- [x] number type
- [x] integer type
- [x] array type
- [x] object type
- [x] definitions/references
- [x] Boolean type
- [x] consts
- [x] `exclusiveMinimum` and `exclusiveMaximum` in integers and
numbers.
- [x] number `multipleOf` keyword
- [x] string `pattern` regex keyword- [ ] `required` keyword
- [ ] `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 + mergeDoes not support:
- ID referencing
- `allOf` and `not` keywords
- conditional keywords `if`, `then` and `else`
- `patternProperties` on objects
- property and schema `dependencies` on objects.