Skip to content

Coretype suggestion: LazyType #8

@oliverpool

Description

@oliverpool

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/itemlist

The //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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions