Skip to main content

Define

pypi version MIT License

Define uses JSON as a language independant way to describe data types that can then be validated, and in the case of all strings form data, cleaned up and turned into the appropriate variable type.

Install

pip install define-oc

Using

Defining data can be done at runtime with dicts and lists, but one of the advantages of creating definition files in JSON is being able to share them with your front end systems to allow validating data with the same rules in the browser or app before even sending it to the server.

user.json

{
	"id": "uuid4",
	"email": {
		"__type__": "string",
		"__regex__": ""
	},
	"name": {
		"first": "string",
		"middle": {
			"__type__": "string",
			"__maximum__": 1,
			"__optional__": true
		},
		"last": "string"
	},
	"address": {
		"line1": "string",
		"line2": {
			"__type__": "string",
			"__optional__": true
		},
		"city": "string",
		"state": {
			"__type__": "string",
			"__regex__": "[A-Z]{2}"
		},
		"country": {
			"__type__": "string",
			"__options__": [ "CA", "MX", "US" ]
		}
	},
	"phone": "string",
	"dob": {
		"__type__": "date",
		"__optional__": true
	},
	"height": {
		"feet": {
			"__type__": "uint",
			"__maximum__": 7
		},
		"inches": {
			"__type__": "uint",
			"__maximum__": 11
		},
		"__optional__": true
	}
}

Once defined, the data can be used in Python using the available classes.

user.py

from define import Parent
import json
import sys

# Load the file
definition = {}
with open('user.json', 'r') as f:
	definition = json.load(f)

# Create the Parent instance
parent = Parent(definition)

# Test data
data = {
	'id': '52cd4b20-ca32-4433-9516-0c8684ec57c2',
	'email': 'chris@domain.com',
	'name': {
		'first': 'Chris',
		'last': 'Nasr'
	},
	'address': {
		'line1': '123 Main Street',
		'state': 'QC',
		'country': 'CA'
	},
	'phone': '(888) 555-1234',
	'height': {
		'feet': '5',
		'inches': '11'
	}
}

if not parent.valid(data):
	print(tree.validation_failures)
	# [ [ 'address.city', 'missing' ] ]

# Clean the data
data = tree.clean(data)
"""
{ ...
  height: {
    'feet': 5,
    'inches': 11
  }
}
"""

Extending

Any fields marked by two leading and trailing underscores is considered a special value and can be accessed using the special method. This can be used to add details only relevent to a specific system, either directly, or through the use of classes that inherit the Define classes.

For example, a class that handles storing the data in a database might need extra data to know how to convert the Define type to an equivalent database type, or to limit that type.

user.json

{
	...
	"name": {
		"first": {
			"__type__": "string",
			"__maximum__": 32,
			"__sql_type__": "varchar(32)"
		},
		"middle": {
			"__type__": "string",
			"__maximum__": 1,
			"__optional__": true,
			"__sql_type__": "char(1)"
		},
		"last": {
			"__type__": "string",
			"__maximum__": 32,
			"__sql_type__": "varchar(32)"
		}
	},
	...
}

Or, if we don't want this data in the shared file, we can add it at runtime and let the class merge the two.

user.py

...
# Create the Parent instance
parent = Parent(definition, {
	'name': {
		'__sql_type__': 'varchar(32)'
	},
	'middle': {
		'__sql_type__': 'char(1)'
	},
	'last': {
		'__sql_type__': 'varchar(32)'
	}
})
...

Then we can access that data at runtime

...
# Get the SQL type for the first name field
name_first_type = parent['name']['first'].special('sql_type')

Documentation

Full documentation, including information on using Arrays and dynamic Objects, as well as how to handle errors, can be found on ouroboroscoding.com/define

Metadata

Release files for define-oc 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for define-oc 1.0.5
File Size Uploaded Provenance Actions
define-oc-1.0.5.tar.gz 21.3 kB Without provenance Details for define-oc-1.0.5.tar.gz

Release files / define-oc-1.0.5.tar.gz

Download URL define-oc-1.0.5.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
eb842485f1a8337092958c9f6bbf8934d70046250aac27e74be192310c8e79b3
BLAKE2b-256 checksum
How to use checksums
38693c43ba232e7a117bf0deb653ea5db675ec766e307cdaf2e6d2a658cb648e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.31.0 rfc3986/1.5.0 tqdm/4.65.0 urllib3/1.26.5 CPython/3.10.12

Security for define-oc 1.0.5

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page