sudoblockio / tackle

Tackle is a programmable configuration language for building modular utilities, code generators, and CLIs with schema validation baked in.
Apache License 2.0
53 stars 2 forks source link

Hook Field Validators #241

Open robcxyz opened 10 months ago

robcxyz commented 10 months ago

Hook Field Validators

Validate fields with custom logic similar to pydantic's validation

Overview

Examples

Simple example where we have an if and return.


H<-:

  foo:

    type: str

    default: bar

    validator:

#      fields:

#        v: Any

#        info: FieldValidationInfo

      fields: ['v', 'info']

      mode:

        enum: ['before', 'after', 'wrap', 'plain']

        default: after  

      body:

        assert->: "{{isinstance(v, 'str')}}"

        return->: v

H<-:

  foo:

    type: str

    default: bar

  validators:

    foo:

      assert->: "{{isinstance(v, 'str')}}"

      return->: v

validator:

  return->: v --if type(v) == 'str' --else raise Wrong type

FieldValidationInfo<-:

  data:

    type: Context

    default->: var public_context

  context:

    type: union[Context]

    default->: var public_context

  Config<-:

    args: ['data', 'context']  

Old


H<-:

  foo:

    type: str

    default: bar

    validators:

      - if: foo is None

        return: ''

      - if: not type(foo, 'str')

        return: ''

      - if: not regex_match(foo, '^[a-zA-Z]')

        return: ''

    validator:

      if: foo is None

      return: ''

H<-:

  type:

    enum:

      - int32

      - int64

    validator:

validator:

  if:

  return:

validator:

  field:

    type: str

    default: v

  info:

    type: FieldValidationInfo

    default: info

  init:

    <-:

      exec:

        var: var var --if var!='item'  

        values: var var --if values!='values'  

  exec: