Skip to content

Custom rules, predicates and expression trees

Michał Kowal edited this page Mar 19, 2024 · 1 revision

Custom rules

Introduction

Custom rules have been created to give the user the possibility to describe custom request validation rules, this way a lot of the validation can be configured from the configuration.

Structure

Currently, custom rules need to be described under rules: and then custom: YAML tags as the list of rules is described by the name and predicate:

rules:
  custom:
    - name: "request payload must contain at least one character and should contain at least one header"
      predicate: "p => LEN(p.payload) > 0 && LEN(p.headers) > 0"

How does it work under the hood?

Whenever the config is edited and there is a change in the custom rules, those are compiled on each WAFFLE run. Then those rules are executed on each request.

Predicate

The predicate structure is simple and is based on the C# lambda predicates. You can divide predicate into 3 parts: <variable> => <expression>

  • variable
  • lambda
  • expression

Currently supported characters and methods:

var (
	tokenVariable = "var"
	tokenFunction = "func"
	tokenNumber   = "token_number"
	tokenField    = "field"
	tokenSpace    = "space"

	tokenLParen           = "("
	tokenRParen           = ")"
	tokenDot              = "."
	tokenDoubleAmpersand  = "&&"
	tokenMoreThan         = ">"
	tokenLessThan         = "<"
	tokenSingleApostrophe = "'"
	specialCharacters     = []string{
		tokenLParen,
		tokenRParen,
		tokenDot,
		tokenDoubleAmpersand,
		tokenMoreThan,
		tokenLessThan,
		tokenSingleApostrophe,
	}

	methodLen    = "LEN"
	methodFormat = "FORMAT"
	methods      = []string{
		methodLen,
		methodFormat,
	}

	fieldPayload = "payload"
	fieldHeaders = "headers"
	fields       = []string{
		fieldPayload,
		fieldHeaders,
	}
)
Clone this wiki locally