Skip to content

Instantly share code, notes, and snippets.

@mficzel
Last active April 20, 2016 13:22
Show Gist options
  • Save mficzel/106a6832f833663f77d10eea5b33fd85 to your computer and use it in GitHub Desktop.
Save mficzel/106a6832f833663f77d10eea5b33fd85 to your computer and use it in GitHub Desktop.
Add CustomTypes to Config Validation

CustomSchema Types

The schema-validation is limited by the current predefined structure. On the other hand the schema files could be a very important source of documentation.

ExampleData:

items:
  foo:
    itemType: 'type1'
    option1: 'option required for type 1'
  bar:
    itemType: 'type2'
    option2: 'option required for type 2'

With the current schema that is an adaption of the concepts of json.schema to yaml no valid schema can be written for that data because it is not possible to require a field if another field has a specific value.

Instead only the presence of the type property could be checked:

type: dictionary
properties:
  items: 
    type: dictionary
    properties:
      itemType: 
        type: string
        required: true
      additionalProperties: TRUE

To write more efficient schemas it would be great to write hierarhical types, flow already has a supertype resolver that could be used to enable hierarchical types.

# register custom types
@itemTypeBase:
  type: dictionary
  properties:
    itemType:
      type: string
      required: true
  additionalProperties: false    

@itemType1:
  superTypes:
    - 'itemTypeBase': TRUE
  properties:
    type: {value: 'type1'}
    option1:
      type: string
      reqired: true
      
@itemType2:
   superTypes:
    - 'itemTypeBase': TRUE
  properties:
    type: {value: 'type2'}
    option2:
      type: string
      reqired: true      

# use the custom types in the schema        
type: dictionary
  properties:
    items: 
      type: [@itemType1, @itemType2]
      description: 'You can define items here'

The implementation would use a registry-stack of the custom-types. If a schema contains typeDefinitions a new instance of the custom-type-stack will be added. The type-definitions are added there and the superTypes are resolved against the other available types. After validating the schema the typeDefinitionStack is reset to the previous state.

A good way to start would be a schema-schema that is currently not possible because of the recursive definition.

Changes to the existing validation

  • Add local typeRegistry that is filled by the @... properties of a schema and that is passed down for complex types
  • Add superTypes for the typeRegistry
  • Add referencing of the local types by @-prefix
  • Add value keyword as an addition to enum
  • Add format: schame-name option for string validation
type: @schema
@schema:
- type: [@simpleSchema, @arraySchema, @numberSchema, @stringSchema ...]
@simpleSchema:
- enum: ['string', 'number', 'integer', 'boolean', 'array', 'dictionary', 'null', 'any' ]
@arraySchema:
type: array
properties:
items: @schema
@complexSchema:
type: dictionary
properties:
type: 'string'
enum: 'array'
disallow: 'array'
description: 'string'
patternProperties:
'@.*': @schema
@numberSchema:
superTypes:
- complexSchema: true
properties:
type: {enum: ['number','integer']}
maximum: number
minimum: number
exclusiveMinimum: number
exclusiveMaximum: number
divisibleBy: number
@stringSchema:
superTypes:
complexSchema: true
properties:
type: {value: 'string'}
pattern: string
minLength: integer
maxLength: integer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment