Skip to content

Commit

Permalink
Document regex functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
joetayloruk committed Mar 12, 2021
1 parent cdfa207 commit d1c477c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/source/user_docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ When this object is translated to a JSON Schema, the `enum` keyword will be used
to define the permitted values of the property.


Patterns
--------

If you need to restrict the values that a UserProperty
can take according to a regex pattern, you can specify this
using the `pattern` parameter::

class Vehicle(UserObject):
rgb_hex_color = UserProperty(str, pattern=r"^[0-9a-f]{6}$")


Note that this will throw a `TypeError` if any type other than `str` is
supplied.

When the object is translated to a JSON Schema, the `pattern` keyword will be used
to validate the permitted values of the property.


Arrays and Dicts
----------------

Expand All @@ -179,6 +197,26 @@ For both SchemaArray and SchemaDict you may pass in any type definition that
you would pass to a UserProperty.


Dict Key Validation via Regex
-----------------------------

If your dictionary only has certain keys that are valid for your application
according to a regex pattern, you can specify this with the parameter
`key_pattern`::

color_names = UserProperty(
SchemaDict(ColorName),
key_pattern=r"^[0-9a-f]{6}$"
)


This will translate to the following in the object's JSON Schema definition::

"propertyNames": {
"pattern": "^[0-9a-f]{6}$"
}


DOM functions
=============

Expand Down
16 changes: 16 additions & 0 deletions docs/source/wysdom.base_schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ SchemaConst
:undoc-members:
:show-inheritance:

SchemaEnum
-------------------------------------

.. autoclass:: wysdom.SchemaEnum
:members:
:undoc-members:
:show-inheritance:

SchemaNone
-------------------------------------

Expand All @@ -33,6 +41,14 @@ SchemaNone
:undoc-members:
:show-inheritance:

SchemaPattern
------------------------------------------

.. autoclass:: wysdom.SchemaPattern
:members:
:undoc-members:
:show-inheritance:

SchemaPrimitive
------------------------------------------

Expand Down

0 comments on commit d1c477c

Please sign in to comment.