string-schema 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- string_schema-0.1.0/LICENSE +21 -0
- string_schema-0.1.0/MANIFEST.in +14 -0
- string_schema-0.1.0/PKG-INFO +545 -0
- string_schema-0.1.0/README.md +490 -0
- string_schema-0.1.0/docs/README.md +75 -0
- string_schema-0.1.0/docs/advanced-usage.md +444 -0
- string_schema-0.1.0/docs/api-reference.md +333 -0
- string_schema-0.1.0/docs/examples.md +271 -0
- string_schema-0.1.0/docs/faq.md +365 -0
- string_schema-0.1.0/docs/getting-started.md +221 -0
- string_schema-0.1.0/docs/pydantic-utilities.md +316 -0
- string_schema-0.1.0/docs/string-syntax.md +376 -0
- string_schema-0.1.0/docs/troubleshooting.md +384 -0
- string_schema-0.1.0/examples/demo.py +301 -0
- string_schema-0.1.0/examples/pydantic_utility_demo.py +247 -0
- string_schema-0.1.0/setup.cfg +4 -0
- string_schema-0.1.0/setup.py +62 -0
- string_schema-0.1.0/simple_schema/__init__.py +108 -0
- string_schema-0.1.0/simple_schema/core/__init__.py +23 -0
- string_schema-0.1.0/simple_schema/core/builders.py +244 -0
- string_schema-0.1.0/simple_schema/core/fields.py +138 -0
- string_schema-0.1.0/simple_schema/core/validators.py +242 -0
- string_schema-0.1.0/simple_schema/examples/__init__.py +36 -0
- string_schema-0.1.0/simple_schema/examples/presets.py +345 -0
- string_schema-0.1.0/simple_schema/examples/recipes.py +380 -0
- string_schema-0.1.0/simple_schema/integrations/__init__.py +15 -0
- string_schema-0.1.0/simple_schema/integrations/json_schema.py +385 -0
- string_schema-0.1.0/simple_schema/integrations/openapi.py +484 -0
- string_schema-0.1.0/simple_schema/integrations/pydantic.py +662 -0
- string_schema-0.1.0/simple_schema/integrations/reverse.py +275 -0
- string_schema-0.1.0/simple_schema/parsing/__init__.py +16 -0
- string_schema-0.1.0/simple_schema/parsing/optimizer.py +246 -0
- string_schema-0.1.0/simple_schema/parsing/string_parser.py +703 -0
- string_schema-0.1.0/simple_schema/parsing/syntax.py +250 -0
- string_schema-0.1.0/simple_schema/utilities.py +501 -0
- string_schema-0.1.0/string_schema.egg-info/PKG-INFO +545 -0
- string_schema-0.1.0/string_schema.egg-info/SOURCES.txt +38 -0
- string_schema-0.1.0/string_schema.egg-info/dependency_links.txt +1 -0
- string_schema-0.1.0/string_schema.egg-info/requires.txt +15 -0
- string_schema-0.1.0/string_schema.egg-info/top_level.txt +2 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 importal
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
include README.md
|
|
2
|
+
include LICENSE
|
|
3
|
+
include MANIFEST.in
|
|
4
|
+
recursive-include docs *.md
|
|
5
|
+
recursive-include examples *.py
|
|
6
|
+
recursive-exclude tests *
|
|
7
|
+
recursive-exclude .venv *
|
|
8
|
+
recursive-exclude .git *
|
|
9
|
+
recursive-exclude .pytest_cache *
|
|
10
|
+
recursive-exclude __pycache__ *
|
|
11
|
+
global-exclude *.pyc
|
|
12
|
+
global-exclude *.pyo
|
|
13
|
+
global-exclude *.pyd
|
|
14
|
+
global-exclude .DS_Store
|
|
@@ -0,0 +1,545 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: string-schema
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simple, LLM-friendly schema definition library
|
|
5
|
+
Home-page: https://github.com/xychenmsn/simple-schema
|
|
6
|
+
Author: importal
|
|
7
|
+
Author-email: xychen@msn.com
|
|
8
|
+
Project-URL: Bug Reports, https://github.com/xychenmsn/simple-schema/issues
|
|
9
|
+
Project-URL: Source, https://github.com/xychenmsn/simple-schema
|
|
10
|
+
Project-URL: Documentation, https://github.com/xychenmsn/simple-schema#readme
|
|
11
|
+
Keywords: schema validation json llm ai data-extraction
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
25
|
+
Classifier: Topic :: Utilities
|
|
26
|
+
Requires-Python: >=3.8
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
License-File: LICENSE
|
|
29
|
+
Requires-Dist: pydantic>=2.0.0
|
|
30
|
+
Requires-Dist: typing-extensions>=4.0.0
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
|
|
34
|
+
Requires-Dist: black>=22.0.0; extra == "dev"
|
|
35
|
+
Requires-Dist: isort>=5.0.0; extra == "dev"
|
|
36
|
+
Requires-Dist: flake8>=5.0.0; extra == "dev"
|
|
37
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
38
|
+
Provides-Extra: docs
|
|
39
|
+
Requires-Dist: sphinx>=5.0.0; extra == "docs"
|
|
40
|
+
Requires-Dist: sphinx-rtd-theme>=1.0.0; extra == "docs"
|
|
41
|
+
Requires-Dist: myst-parser>=0.18.0; extra == "docs"
|
|
42
|
+
Dynamic: author
|
|
43
|
+
Dynamic: author-email
|
|
44
|
+
Dynamic: classifier
|
|
45
|
+
Dynamic: description
|
|
46
|
+
Dynamic: description-content-type
|
|
47
|
+
Dynamic: home-page
|
|
48
|
+
Dynamic: keywords
|
|
49
|
+
Dynamic: license-file
|
|
50
|
+
Dynamic: project-url
|
|
51
|
+
Dynamic: provides-extra
|
|
52
|
+
Dynamic: requires-dist
|
|
53
|
+
Dynamic: requires-python
|
|
54
|
+
Dynamic: summary
|
|
55
|
+
|
|
56
|
+
# Simple Schema
|
|
57
|
+
|
|
58
|
+
A simple, LLM-friendly schema definition library for Python that converts intuitive string syntax into structured data schemas.
|
|
59
|
+
|
|
60
|
+
## ๐ Quick Start
|
|
61
|
+
|
|
62
|
+
### Installation
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install simple-schema
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 30-Second Example
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from simple_schema import string_to_json_schema
|
|
72
|
+
|
|
73
|
+
# Define a schema with simple, readable syntax
|
|
74
|
+
schema = string_to_json_schema("name:string, email:email, age:int?")
|
|
75
|
+
|
|
76
|
+
# Get a complete JSON Schema ready for validation
|
|
77
|
+
print(schema)
|
|
78
|
+
# {
|
|
79
|
+
# "type": "object",
|
|
80
|
+
# "properties": {
|
|
81
|
+
# "name": {"type": "string"},
|
|
82
|
+
# "email": {"type": "string", "format": "email"},
|
|
83
|
+
# "age": {"type": "integer"}
|
|
84
|
+
# },
|
|
85
|
+
# "required": ["name", "email"]
|
|
86
|
+
# }
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Create Pydantic Models Directly
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from simple_schema import string_to_model
|
|
93
|
+
|
|
94
|
+
# Create Pydantic model from string syntax
|
|
95
|
+
UserModel = string_to_model("name:string, email:email, age:int?")
|
|
96
|
+
|
|
97
|
+
# Use immediately
|
|
98
|
+
user = UserModel(name="Alice", email="alice@example.com")
|
|
99
|
+
print(user.model_dump_json()) # {"name": "Alice", "email": "alice@example.com"}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## ๐ฏ What Simple Schema Does
|
|
103
|
+
|
|
104
|
+
Simple Schema takes human-readable text descriptions and converts them into structured schemas for data validation, extraction, and API documentation. Perfect for LLM data extraction, API development, and configuration validation.
|
|
105
|
+
|
|
106
|
+
**Input:** Human-readable string syntax
|
|
107
|
+
**Output:** JSON Schema, Pydantic models, or OpenAPI specifications
|
|
108
|
+
|
|
109
|
+
## ๐ Core Functions & Use Cases
|
|
110
|
+
|
|
111
|
+
### ๐ Schema Conversion Matrix
|
|
112
|
+
|
|
113
|
+
**Forward Conversions (Source โ Target):**
|
|
114
|
+
|
|
115
|
+
| Function | Input | Output | Use Case |
|
|
116
|
+
| -------------------------- | ---------------- | -------------------- | ------------------------------------------- |
|
|
117
|
+
| `string_to_json_schema()` | String syntax | JSON Schema dict | **Main conversion** - string to JSON Schema |
|
|
118
|
+
| `string_to_model()` | String syntax | Pydantic model class | **Direct path** - string to Pydantic model |
|
|
119
|
+
| `string_to_model_code()` | String syntax | Python code string | **Code generation** - for templates |
|
|
120
|
+
| `string_to_openapi()` | String syntax | OpenAPI schema dict | **Direct path** - string to OpenAPI |
|
|
121
|
+
| `json_schema_to_model()` | JSON Schema dict | Pydantic model class | When you already have JSON Schema |
|
|
122
|
+
| `json_schema_to_openapi()` | JSON Schema dict | OpenAPI schema dict | When you already have JSON Schema |
|
|
123
|
+
|
|
124
|
+
**Reverse Conversions (Target โ Source):**
|
|
125
|
+
|
|
126
|
+
| Function | Input | Output | Use Case |
|
|
127
|
+
| -------------------------- | ---------------- | ---------------- | ------------------------------------------ |
|
|
128
|
+
| `model_to_string()` | Pydantic model | String syntax | **Schema introspection** - model to string |
|
|
129
|
+
| `model_to_json_schema()` | Pydantic model | JSON Schema dict | **Export** - model to JSON Schema |
|
|
130
|
+
| `json_schema_to_string()` | JSON Schema dict | String syntax | **Migration** - JSON Schema to string |
|
|
131
|
+
| `openapi_to_string()` | OpenAPI schema | String syntax | **Import** - OpenAPI to string |
|
|
132
|
+
| `openapi_to_json_schema()` | OpenAPI schema | JSON Schema dict | **Conversion** - OpenAPI to JSON Schema |
|
|
133
|
+
|
|
134
|
+
### ๐ Data Validation Functions
|
|
135
|
+
|
|
136
|
+
| Function | Input | Output | Use Case |
|
|
137
|
+
| -------------------------- | ------------- | ----------------- | --------------------------------- |
|
|
138
|
+
| `validate_to_dict()` | Data + schema | Validated dict | **API responses** - clean dicts |
|
|
139
|
+
| `validate_to_model()` | Data + schema | Pydantic model | **Business logic** - typed models |
|
|
140
|
+
| `validate_string_syntax()` | String syntax | Validation result | Check syntax and get feedback |
|
|
141
|
+
|
|
142
|
+
### ๐จ Function Decorators
|
|
143
|
+
|
|
144
|
+
| Decorator | Purpose | Returns | Use Case |
|
|
145
|
+
| ------------------ | ---------------------- | -------------- | ------------------ |
|
|
146
|
+
| `@returns_dict()` | Auto-validate to dict | Validated dict | **API endpoints** |
|
|
147
|
+
| `@returns_model()` | Auto-validate to model | Pydantic model | **Business logic** |
|
|
148
|
+
|
|
149
|
+
### ๐ง Utility Functions
|
|
150
|
+
|
|
151
|
+
| Function | Purpose | Returns | Use Case |
|
|
152
|
+
| --------------------------------- | ------------------- | ------------------ | ------------------------ |
|
|
153
|
+
| `get_model_info()` | Model introspection | Model details dict | **Debugging & analysis** |
|
|
154
|
+
| `validate_schema_compatibility()` | Schema validation | Compatibility info | **Schema validation** |
|
|
155
|
+
|
|
156
|
+
### ๐ฏ Key Scenarios
|
|
157
|
+
|
|
158
|
+
- **๐ค LLM Data Extraction**: Define extraction schemas that LLMs can easily follow
|
|
159
|
+
- **๐ง API Development**: Generate Pydantic models and OpenAPI docs from simple syntax
|
|
160
|
+
- **โ
Data Validation**: Create robust validation schemas with minimal code
|
|
161
|
+
- **๐ Configuration**: Define and validate application configuration schemas
|
|
162
|
+
- **๐ Data Transformation**: Convert between different schema formats
|
|
163
|
+
|
|
164
|
+
### โ
Validate Data
|
|
165
|
+
|
|
166
|
+
```python
|
|
167
|
+
from simple_schema import validate_to_dict, validate_to_model
|
|
168
|
+
|
|
169
|
+
# Example raw data (from API, user input, etc.)
|
|
170
|
+
raw_data = {
|
|
171
|
+
"name": "John Doe",
|
|
172
|
+
"email": "john@example.com",
|
|
173
|
+
"age": "25", # String that needs conversion
|
|
174
|
+
"extra_field": "ignored" # Will be filtered out
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
# Validate to clean dictionaries (perfect for API responses)
|
|
178
|
+
user_dict = validate_to_dict(raw_data, "name:string, email:email, age:int?")
|
|
179
|
+
print(user_dict) # {"name": "John Doe", "email": "john@example.com", "age": 25}
|
|
180
|
+
|
|
181
|
+
# Validate to typed models (perfect for business logic)
|
|
182
|
+
user_model = validate_to_model(raw_data, "name:string, email:email, age:int?")
|
|
183
|
+
print(user_model.name) # "John Doe" - Full type safety
|
|
184
|
+
print(user_model.age) # 25 - Converted to int
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### ๐จ Function Decorators
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from simple_schema import returns_dict, returns_model
|
|
191
|
+
import uuid
|
|
192
|
+
|
|
193
|
+
# Auto-validate function returns to dicts
|
|
194
|
+
@returns_dict("id:string, name:string, active:bool")
|
|
195
|
+
def create_user(name):
|
|
196
|
+
# Input: "Alice"
|
|
197
|
+
# Function returns unvalidated dict
|
|
198
|
+
return {"id": str(uuid.uuid4()), "name": name, "active": True, "extra": "ignored"}
|
|
199
|
+
# Output: {"id": "123e4567-...", "name": "Alice", "active": True}
|
|
200
|
+
# Note: 'extra' field filtered out, types validated
|
|
201
|
+
|
|
202
|
+
# Auto-validate function returns to models
|
|
203
|
+
@returns_model("name:string, email:string")
|
|
204
|
+
def process_user(raw_input):
|
|
205
|
+
# Input: {"name": "Bob", "email": "bob@test.com", "junk": "data"}
|
|
206
|
+
# Function returns unvalidated dict
|
|
207
|
+
return {"name": raw_input["name"], "email": raw_input["email"], "junk": "data"}
|
|
208
|
+
# Output: UserModel(name="Bob", email="bob@test.com")
|
|
209
|
+
# Note: Returns typed Pydantic model, 'junk' field filtered out
|
|
210
|
+
|
|
211
|
+
# Usage examples:
|
|
212
|
+
user_dict = create_user("Alice") # Returns validated dict
|
|
213
|
+
user_model = process_user({"name": "Bob", "email": "bob@test.com"}) # Returns Pydantic model
|
|
214
|
+
print(user_model.name) # "Bob" - Full type safety
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### ๐ FastAPI Integration
|
|
218
|
+
|
|
219
|
+
```python
|
|
220
|
+
from simple_schema import string_to_model, returns_dict
|
|
221
|
+
|
|
222
|
+
# Create models for FastAPI
|
|
223
|
+
UserRequest = string_to_model("name:string, email:email")
|
|
224
|
+
|
|
225
|
+
@app.post("/users")
|
|
226
|
+
@returns_dict("id:int, name:string, email:string")
|
|
227
|
+
def create_user_endpoint(user: UserRequest):
|
|
228
|
+
return {"id": 123, "name": user.name, "email": user.email}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
**Features**: Arrays `[{name:string}]`, nested objects `{profile:{bio:text?}}`, enums, constraints, decorators.
|
|
232
|
+
|
|
233
|
+
[๐ **Complete Documentation**](docs/pydantic-utilities.md)
|
|
234
|
+
|
|
235
|
+
## ๐ More Examples
|
|
236
|
+
|
|
237
|
+
### ๐ฑ Simple Example - Basic User Data
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
from simple_schema import string_to_json_schema
|
|
241
|
+
|
|
242
|
+
# Define schema using intuitive string syntax
|
|
243
|
+
schema = string_to_json_schema("""
|
|
244
|
+
name:string(min=1, max=100),
|
|
245
|
+
email:email,
|
|
246
|
+
age:int(0, 120)?,
|
|
247
|
+
active:bool
|
|
248
|
+
""")
|
|
249
|
+
|
|
250
|
+
# Result: JSON Schema dictionary ready for validation
|
|
251
|
+
print(schema)
|
|
252
|
+
# {
|
|
253
|
+
# "type": "object",
|
|
254
|
+
# "properties": {
|
|
255
|
+
# "name": {"type": "string", "minLength": 1, "maxLength": 100},
|
|
256
|
+
# "email": {"type": "string", "format": "email"},
|
|
257
|
+
# "age": {"type": "integer", "minimum": 0, "maximum": 120},
|
|
258
|
+
# "active": {"type": "boolean"}
|
|
259
|
+
# },
|
|
260
|
+
# "required": ["name", "email", "active"]
|
|
261
|
+
# }
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### ๐ณ Moderately Complex Example - Product Data
|
|
265
|
+
|
|
266
|
+
```python
|
|
267
|
+
# Product with arrays and nested objects
|
|
268
|
+
schema = string_to_json_schema("""
|
|
269
|
+
{
|
|
270
|
+
id:uuid,
|
|
271
|
+
name:string,
|
|
272
|
+
price:number(min=0),
|
|
273
|
+
tags:[string]?,
|
|
274
|
+
inventory:{
|
|
275
|
+
in_stock:bool,
|
|
276
|
+
quantity:int?
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
""")
|
|
280
|
+
|
|
281
|
+
# Result: Complete JSON Schema with nested objects and arrays
|
|
282
|
+
print(schema) # Full JSON Schema ready for validation
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
> ๐ **For more examples and advanced syntax**, see our [detailed documentation](docs/string-syntax.md)
|
|
286
|
+
|
|
287
|
+
## ๐ Output Formats & Results
|
|
288
|
+
|
|
289
|
+
### ๐ Pydantic Models (Python Classes)
|
|
290
|
+
|
|
291
|
+
```python
|
|
292
|
+
from simple_schema import string_to_model
|
|
293
|
+
|
|
294
|
+
# Direct conversion: String syntax โ Pydantic model
|
|
295
|
+
UserModel = string_to_model("name:string, email:email, active:bool")
|
|
296
|
+
|
|
297
|
+
# Use the model for validation
|
|
298
|
+
user = UserModel(name="John Doe", email="john@example.com", active=True)
|
|
299
|
+
print(user.model_dump_json()) # {"name": "John Doe", "email": "john@example.com", "active": true}
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### ๐ง Code Generation (For Templates & Tools)
|
|
303
|
+
|
|
304
|
+
```python
|
|
305
|
+
from simple_schema import string_to_model_code
|
|
306
|
+
|
|
307
|
+
# Generate Pydantic model code as a string
|
|
308
|
+
code = string_to_model_code("User", "name:string, email:email, active:bool")
|
|
309
|
+
print(code)
|
|
310
|
+
|
|
311
|
+
# Output:
|
|
312
|
+
# from pydantic import BaseModel
|
|
313
|
+
# from typing import Optional
|
|
314
|
+
#
|
|
315
|
+
# class User(BaseModel):
|
|
316
|
+
# name: str
|
|
317
|
+
# email: str
|
|
318
|
+
# active: bool
|
|
319
|
+
|
|
320
|
+
# Perfect for code generators, templates, or saving to files
|
|
321
|
+
with open('models.py', 'w') as f:
|
|
322
|
+
f.write(code)
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### ๐ OpenAPI Schemas (API Documentation)
|
|
326
|
+
|
|
327
|
+
```python
|
|
328
|
+
from simple_schema import string_to_openapi
|
|
329
|
+
|
|
330
|
+
# Direct conversion: String syntax โ OpenAPI schema
|
|
331
|
+
openapi_schema = string_to_openapi("name:string, email:email")
|
|
332
|
+
print(openapi_schema)
|
|
333
|
+
# {
|
|
334
|
+
# "type": "object",
|
|
335
|
+
# "properties": {
|
|
336
|
+
# "name": {"type": "string"},
|
|
337
|
+
# "email": {"type": "string", "format": "email"}
|
|
338
|
+
# },
|
|
339
|
+
# "required": ["name", "email"]
|
|
340
|
+
# }
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
## ๐ Reverse Conversions (Universal Schema Converter)
|
|
344
|
+
|
|
345
|
+
Simple Schema provides complete bidirectional conversion between all schema formats!
|
|
346
|
+
|
|
347
|
+
> **โ ๏ธ Information Loss Notice**: Reverse conversions (from JSON Schema/OpenAPI/Pydantic back to string syntax) may lose some information due to format differences. However, the resulting schemas are designed to cover the most common use cases and maintain functional equivalence for typical validation scenarios.
|
|
348
|
+
|
|
349
|
+
### ๐ Schema Introspection
|
|
350
|
+
|
|
351
|
+
```python
|
|
352
|
+
from simple_schema import model_to_string, string_to_model
|
|
353
|
+
|
|
354
|
+
# Create a model first
|
|
355
|
+
UserModel = string_to_model("name:string, email:email, active:bool")
|
|
356
|
+
|
|
357
|
+
# Reverse engineer it back to string syntax
|
|
358
|
+
schema_string = model_to_string(UserModel)
|
|
359
|
+
print(f"Model schema: {schema_string}")
|
|
360
|
+
# Output: "name:string, email:email, active:bool"
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### ๐ฆ Migration & Import
|
|
364
|
+
|
|
365
|
+
```python
|
|
366
|
+
from simple_schema import json_schema_to_string
|
|
367
|
+
|
|
368
|
+
# Example: Convert existing JSON Schema to Simple Schema syntax
|
|
369
|
+
json_schema = {
|
|
370
|
+
"type": "object",
|
|
371
|
+
"properties": {
|
|
372
|
+
"name": {"type": "string"},
|
|
373
|
+
"email": {"type": "string", "format": "email"},
|
|
374
|
+
"age": {"type": "integer"}
|
|
375
|
+
},
|
|
376
|
+
"required": ["name", "email"]
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
simple_syntax = json_schema_to_string(json_schema)
|
|
380
|
+
print(f"Converted: {simple_syntax}")
|
|
381
|
+
# Output: "name:string, email:email, age:int?"
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
### ๐ง Schema Comparison & Analysis
|
|
385
|
+
|
|
386
|
+
```python
|
|
387
|
+
from simple_schema import string_to_model, model_to_string
|
|
388
|
+
|
|
389
|
+
# Create two versions of a model
|
|
390
|
+
UserV1 = string_to_model("name:string, email:email")
|
|
391
|
+
UserV2 = string_to_model("name:string, email:email, active:bool")
|
|
392
|
+
|
|
393
|
+
# Compare them
|
|
394
|
+
v1_str = model_to_string(UserV1)
|
|
395
|
+
v2_str = model_to_string(UserV2)
|
|
396
|
+
|
|
397
|
+
print("Schema changes:")
|
|
398
|
+
print(f"V1: {v1_str}") # "name:string, email:email"
|
|
399
|
+
print(f"V2: {v2_str}") # "name:string, email:email, active:bool"
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
## ๐จ String Syntax Reference
|
|
403
|
+
|
|
404
|
+
### Basic Types
|
|
405
|
+
|
|
406
|
+
- `string`, `int`, `number`, `bool` โ Basic data types
|
|
407
|
+
- `email`, `url`, `datetime`, `date`, `uuid`, `phone` โ Special validated types
|
|
408
|
+
|
|
409
|
+
### Field Modifiers
|
|
410
|
+
|
|
411
|
+
- `field_name:type` โ Required field
|
|
412
|
+
- `field_name:type?` โ Optional field
|
|
413
|
+
- `field_name:type(constraints)` โ Field with validation
|
|
414
|
+
|
|
415
|
+
### Common Patterns
|
|
416
|
+
|
|
417
|
+
- `string(min=1, max=100)` โ Length constraints
|
|
418
|
+
- `int(0, 120)` โ Range constraints
|
|
419
|
+
- `[string]` โ Simple arrays
|
|
420
|
+
- `[{name:string, email:email}]` โ Object arrays
|
|
421
|
+
- `status:enum(active, inactive)` โ Enum values
|
|
422
|
+
- `id:string|uuid` โ Union types
|
|
423
|
+
|
|
424
|
+
> ๐ **Complete syntax guide**: See [docs/string-syntax.md](docs/string-syntax.md) for full reference
|
|
425
|
+
|
|
426
|
+
## โ
Validation
|
|
427
|
+
|
|
428
|
+
```python
|
|
429
|
+
from simple_schema import validate_string_syntax
|
|
430
|
+
|
|
431
|
+
# Validate your schema syntax
|
|
432
|
+
result = validate_string_syntax("name:string, email:email, age:int?")
|
|
433
|
+
|
|
434
|
+
print(f"Valid: {result['valid']}") # True
|
|
435
|
+
print(f"Features used: {result['features_used']}") # ['basic_types', 'optional_fields']
|
|
436
|
+
print(f"Field count: {len(result['parsed_fields'])}") # 3
|
|
437
|
+
|
|
438
|
+
# Example with invalid syntax
|
|
439
|
+
bad_result = validate_string_syntax("name:invalid_type")
|
|
440
|
+
print(f"Valid: {bad_result['valid']}") # False
|
|
441
|
+
print(f"Errors: {bad_result['errors']}") # ['Unknown type: invalid_type']
|
|
442
|
+
```
|
|
443
|
+
|
|
444
|
+
## ๐๏ธ Common Use Cases
|
|
445
|
+
|
|
446
|
+
### ๐ค LLM Data Extraction
|
|
447
|
+
|
|
448
|
+
```python
|
|
449
|
+
from simple_schema import string_to_json_schema
|
|
450
|
+
|
|
451
|
+
# Define what data to extract
|
|
452
|
+
schema = string_to_json_schema("company:string, employees:[{name:string, email:email}], founded:int?")
|
|
453
|
+
# Use schema in LLM prompts for structured data extraction
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
### ๐ง FastAPI Development
|
|
457
|
+
|
|
458
|
+
```python
|
|
459
|
+
from simple_schema import string_to_model
|
|
460
|
+
|
|
461
|
+
# Create model for FastAPI
|
|
462
|
+
UserModel = string_to_model("name:string, email:email")
|
|
463
|
+
|
|
464
|
+
# Use in FastAPI endpoints
|
|
465
|
+
@app.post("/users/")
|
|
466
|
+
async def create_user(user: UserModel):
|
|
467
|
+
return {"id": 123, "name": user.name, "email": user.email}
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
### ๐๏ธ Code Generation & Templates
|
|
471
|
+
|
|
472
|
+
```python
|
|
473
|
+
from simple_schema import string_to_model_code
|
|
474
|
+
|
|
475
|
+
# Generate model code
|
|
476
|
+
code = string_to_model_code("User", "name:string, email:email")
|
|
477
|
+
print(code)
|
|
478
|
+
# Output: Complete Pydantic model class as string
|
|
479
|
+
|
|
480
|
+
# Save to file
|
|
481
|
+
with open('user_model.py', 'w') as f:
|
|
482
|
+
f.write(code)
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
### ๐ Configuration Validation
|
|
486
|
+
|
|
487
|
+
```python
|
|
488
|
+
from simple_schema import string_to_json_schema
|
|
489
|
+
|
|
490
|
+
# Validate app configuration
|
|
491
|
+
config_schema = string_to_json_schema("database:{host:string, port:int}, debug:bool")
|
|
492
|
+
# Use for validating config files
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
## ๐ Documentation
|
|
496
|
+
|
|
497
|
+
- **[Getting Started](docs/getting-started.md)** - Quick start guide and installation
|
|
498
|
+
- **[String Syntax Guide](docs/string-syntax.md)** - Complete syntax reference
|
|
499
|
+
- **[API Reference](docs/api-reference.md)** - Full API documentation
|
|
500
|
+
- **[Examples](docs/examples.md)** - Practical examples and patterns
|
|
501
|
+
|
|
502
|
+
## ๐ Example Schemas
|
|
503
|
+
|
|
504
|
+
Ready-to-use schema examples are available in the `examples/` directory:
|
|
505
|
+
|
|
506
|
+
```python
|
|
507
|
+
# Import example schemas if needed
|
|
508
|
+
from simple_schema.examples.presets import user_schema, product_schema
|
|
509
|
+
from simple_schema.examples.recipes import create_ecommerce_product_schema
|
|
510
|
+
|
|
511
|
+
# Or better yet, use string syntax directly:
|
|
512
|
+
user_schema = string_to_json_schema("name:string, email:email, age:int?")
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
## ๐งช Testing
|
|
516
|
+
|
|
517
|
+
The library includes comprehensive tests covering all functionality:
|
|
518
|
+
|
|
519
|
+
```bash
|
|
520
|
+
# Run tests (requires pytest)
|
|
521
|
+
pip install pytest
|
|
522
|
+
pytest tests/
|
|
523
|
+
|
|
524
|
+
# Results: 66 tests passed, 3 skipped (Pydantic tests when not installed)
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
## ๐ค Contributing
|
|
528
|
+
|
|
529
|
+
Contributions are welcome! The codebase is well-organized and documented:
|
|
530
|
+
|
|
531
|
+
```
|
|
532
|
+
simple_schema/
|
|
533
|
+
โโโ core/ # Core functionality (fields, builders, validators)
|
|
534
|
+
โโโ parsing/ # String parsing and syntax
|
|
535
|
+
โโโ integrations/ # Pydantic, JSON Schema, OpenAPI
|
|
536
|
+
โโโ examples/ # Built-in schemas and recipes
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
## ๐ License
|
|
540
|
+
|
|
541
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
|
542
|
+
|
|
543
|
+
---
|
|
544
|
+
|
|
545
|
+
**Simple Schema** - Making data validation simple, intuitive, and LLM-friendly! ๐
|