Skip to main content

schema-markdown

PyPI - Status PyPI GitHub PyPI - Python Version

schema-markdown is a schema definition and validation library.

Links

Define a Schema

Schemas are defined using the Schema Markdown language, which is parsed by the parse_schema_markdown function. For example:

from schema_markdown import parse_schema_markdown

model_types = parse_schema_markdown('''\
# An aggregate numerical operation
struct Aggregation

    # The aggregation function - default is "Sum"
    optional AggregationFunction aggregation

    # The numbers to aggregate on
    int[len > 0] numbers

# An aggregation function
enum AggregationFunction
    Average
    Sum
''')

Validate using a Schema

To validate an object using the schema, use the validate_type function. For example:

from schema_markdown import validate_type

validate_type(model_types, 'Aggregation', {'numbers': [1, 2, '3', 4]})

{'numbers': [1, 2, 3, 4]}

Notice that the numerical input '3' above is type-massaged to the integer 3 by validation.

Validation fails if the object does not match the schema:

from schema_markdown import ValidationError

try:
    validate_type(model_types, 'Aggregation', {'numbers': [1, 2, 'asdf', 4]})
except ValidationError as exc:
    str(exc)

'Invalid value "asdf" (type "str") for member "numbers.2", expected type "int"'

Validation also fails if a member constraint is violated:

try:
    validate_type(model_types, 'Aggregation', {'numbers': []})
except ValidationError as exc:
    str(exc)

'Invalid value [] (type "list") for member "numbers", expected type "array" [len > 0]'

validate_type validates JSON-like values. In this Python implementation, date values are datetime.date, datetime values are datetime.datetime, and uuid strings remain strings. datetime.date, datetime.datetime, and uuid.UUID inputs are left unchanged. See The Schema Markdown Language - Built-in Types for accepted JSON input formats and host-runtime behavior.

Schema Documentation

You can document a schema with BareScript's schemaDoc application running on the MarkdownUp application.

If your type model JSON file or Schema Markdown file is publicly visible, you can use the BareScript model application with the var.vURL query argument:

https://craigahobbs.github.io/bare-script-py/model/#var.vURL='https://craigahobbs.github.io/bare-script-py/library/model.json'

Note: Schema Markdown files use the .smd file extension.

Self-Hosting Schema Documentation

You can host the schemaDoc application yourself by adding a schemaDoc HTML stub:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>MarkdownUp</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://craigahobbs.github.io/markdown-up/app.css">
        <link rel="preload" href="https://craigahobbs.github.io/markdown-up/bare-script/static/markdown.css" as="style">
        <link rel="modulepreload" href="https://craigahobbs.github.io/markdown-up/lib/appImports.js" as="script">
    </head>
    <script type="module">
        import {MarkdownUp} from 'https://craigahobbs.github.io/markdown-up/lib/appImports.js';
        const app = new MarkdownUp(window, {
            'markdownText': `\
~~~markdown-script
include <schemaDoc.bare>

schemaDocMain('model.json', 'My Model')
~~~
`
        });
        app.run();
    </script>
</html>

To view locally, start a local static web server:

python3 -m http.server

Development

This package is developed using python-build. It was started using python-template as follows:

template-specialize python-template/template/ schema-markdown/ -k package schema-markdown -k name 'Craig A. Hobbs' -k email 'craigahobbs@gmail.com' -k github 'craigahobbs' -k nomain 1

Release files for schema-markdown 1.3.2

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

Source distribution (sdist)

Source distribution for schema-markdown 1.3.2
File Size Uploaded
schema_markdown-1.3.2.tar.gz 18.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for schema-markdown 1.3.2
File Interpreter ABI Platform
schema_markdown-1.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 38.6 kB

Release files / schema_markdown-1.3.2.tar.gz

Download URL schema_markdown-1.3.2.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
54bfac422fda89f796d1bfd96bb87a6a7bef486c5254079732bbe92321721bb2
BLAKE2b-256 checksum
How to use checksums
2d7d559209378b398d6931e13563e2529ef35969d22e82eeabfb72660ec39c10
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release files / schema_markdown-1.3.2-py3-none-any.whl

Download URL schema_markdown-1.3.2-py3-none-any.whl
Size 19.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b7cf77709d9bf1c4eaad068ca603b6cd2c621de8670abf4878ec51a60d93d5cf
BLAKE2b-256 checksum
How to use checksums
4e71ff34535b1390b070082db1119cea508789cca46b3a8436902879d9205dfd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.6

Release history Release notifications | RSS feed

1.3.3

2 release files

This release

1.3.2 This release

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.13

2 release files

1.2.12

2 release files

1.2.9

2 release files

1.2.8

2 release files

1.2.7

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.2

2 release files

1.2.1

1 release file

1.2.0

1 release file

1.1.12

1 release file

1.1.11

1 release file

1.1.10

1 release file

1.1.9

1 release file

1.1.8

1 release file

1.1.7

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.3

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0.0

1 release file

0.9.16

1 release file

0.9.15

1 release file

0.9.14

1 release file

0.9.13

1 release file

0.9.12

1 release file

0.9.11

1 release file

0.9.10

1 release file

0.9.9

1 release file

0.9.8

1 release file

0.9.7

1 release file

0.9.6

1 release file

0.9.5

1 release file

0.9.4

1 release file

0.9.3

1 release file

0.9.2

1 release file

0.9.1

1 release file

0.9

1 release file

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