-
Notifications
You must be signed in to change notification settings - Fork 53
Open
Labels
Description
Hi,
I have a suggestion for a new coretype.
I want to validate a recursive schema (an item list where each item can itself be an item list).
Instead of creating a //recursive type, I propose a //lazy type:
# /.meta/lazy, the schema for //lazy definitions:
{
"type": "//rec",
"required": {
"type": { "type": "//str", "value": "//lazy" },
"of": { "type": "//str" }
}
}Example if we define a new /example/itemlist type (a list of elements, that are either string or itemlist):
type: //seq
contents:
- //str
- type: //lazy
of: /example/itemlistThe //lazy type should not be resolved during the schema construction, but at validation time.
Implementation example in python:
class LazyType(_CoreType):
@staticmethod
def subname(): return 'lazy'
def __init__(self, schema, rx):
if not {'type', 'of'}.issuperset(schema):
raise SchemaError('unknown parameter for //lazy')
if not schema.get('of'):
raise SchemaError('no actual type provided for //lazy')
self.schema = schema['of']
self.rx = rx
def validate(self, value, name='value'):
validator = self.rx.make_schema(self.schema)
validator.validate(value)More than recursive schema, it allows to use a schema that will be later defined (in case of circular inclusion)
Reactions are currently unavailable