Add your own rules

You can add your own rules by using Kensho.rule.add(). Also, You have to set to return boolean type in the callback function. It meaning is the value is valid or invalid.

Kensho.rule.add('myRule', (value, option, Kensho) => {
    let returnValue = false
    // do something to validate..
    return returnValue
})

The 1st argument is rule name. The 2nd argument is callback function as your own rule validation.

callback function arguments

Arguments

Type

Description

value

any

Value to be validated.

option

object

Callback option.

Kensho

Kensho

Kensho class.

With TypeScript

/** @todo */

// export your rule types store that extends RuleTypeStore
export interface MyRuleTypeStore extends RuleTypeStore {
  'myRule' : RuleType<string, {}>
}

// 
export const myRule: MyRuleTypeStore['myRule'] = (value, opt) => {
  return true
}

// Add the 'myRule' rule.
Kensho.rule.add('myRule', myRule)

const myRule = Kensho.rule.get<'myRule', MyRuleTypeStore>('myRule')

Last updated