2022-10-23 01:10:00 +02:00
|
|
|
import { JSONPathCopy } from './../utils/json.ts'
|
|
|
|
import type { JSONObject, JSONPrimitive, JSONArray } from './../utils/json.ts'
|
2022-09-20 03:11:34 +02:00
|
|
|
import { rule } from './rule.ts'
|
|
|
|
import { sanitizer } from './sanitizer.ts'
|
2022-10-23 01:10:00 +02:00
|
|
|
import type { Schema } from './schema.ts'
|
2022-09-20 03:11:34 +02:00
|
|
|
|
2022-11-01 06:22:04 +01:00
|
|
|
type Target = 'query' | 'queries' | 'header' | 'body' | 'json'
|
2022-09-30 14:56:39 +02:00
|
|
|
type Type = JSONPrimitive | JSONObject | JSONArray | File
|
2022-10-19 12:17:04 +02:00
|
|
|
type RuleFunc = (value: Type) => boolean
|
|
|
|
type Rule = {
|
|
|
|
name: string
|
|
|
|
func: RuleFunc
|
|
|
|
customMessage?: string
|
|
|
|
type: 'type' | 'value'
|
|
|
|
}
|
2022-09-20 03:11:34 +02:00
|
|
|
type Sanitizer = (value: Type) => Type
|
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
export type ValidateResult = {
|
|
|
|
isValid: boolean
|
|
|
|
message: string | undefined
|
2022-10-31 16:03:17 +01:00
|
|
|
target: Target | 'unknown'
|
|
|
|
key: string | null
|
|
|
|
value: Type | null
|
2022-10-19 12:17:04 +02:00
|
|
|
ruleName: string
|
|
|
|
ruleType: 'type' | 'value'
|
|
|
|
jsonData?: JSONObject
|
|
|
|
}
|
|
|
|
|
2022-10-12 23:48:59 +02:00
|
|
|
export abstract class VObjectBase<T extends Schema> {
|
|
|
|
container: T
|
|
|
|
keys: string[] = []
|
|
|
|
protected _isOptional: boolean = false
|
|
|
|
constructor(container: T, key: string) {
|
|
|
|
this.container = container
|
|
|
|
if (this instanceof VArray) {
|
|
|
|
this.keys.push(key, '[*]')
|
|
|
|
} else if (this instanceof VObject) {
|
|
|
|
this.keys.push(key)
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 12:17:04 +02:00
|
|
|
|
2022-10-12 23:48:59 +02:00
|
|
|
isOptional() {
|
|
|
|
this._isOptional = true
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
getValidators = (): VBase[] => {
|
|
|
|
const validators: VBase[] = []
|
|
|
|
|
|
|
|
const walk = (container: T, keys: string[], isOptional: boolean) => {
|
|
|
|
for (const v of Object.values(container)) {
|
|
|
|
if (v instanceof VArray || v instanceof VObject) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
isOptional ||= v._isOptional
|
2022-11-29 14:02:20 +01:00
|
|
|
walk(v.container as T, [...keys, ...v.keys], isOptional)
|
2022-10-12 23:48:59 +02:00
|
|
|
} else if (v instanceof VBase) {
|
|
|
|
if (isOptional) v.isOptional()
|
|
|
|
v.baseKeys.push(...keys)
|
|
|
|
validators.push(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
walk(this.container, this.keys, this._isOptional)
|
|
|
|
|
|
|
|
return validators
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class VObject<T extends Schema> extends VObjectBase<T> {
|
|
|
|
constructor(container: T, key: string) {
|
|
|
|
super(container, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class VArray<T extends Schema> extends VObjectBase<T> {
|
|
|
|
type: 'array' = 'array'
|
|
|
|
constructor(container: T, key: string) {
|
|
|
|
super(container, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
export class Validator {
|
2022-11-28 00:45:57 +01:00
|
|
|
constructor(private inArray: boolean = false) {}
|
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
query = (key: string): VString => new VString({ target: 'query', key: key })
|
2022-11-01 06:22:04 +01:00
|
|
|
queries = (key: string): VStringArray => new VStringArray({ target: 'queries', key: key })
|
2022-09-20 03:11:34 +02:00
|
|
|
header = (key: string): VString => new VString({ target: 'header', key: key })
|
|
|
|
body = (key: string): VString => new VString({ target: 'body', key: key })
|
2022-10-12 23:48:59 +02:00
|
|
|
json = (key: string) => {
|
2022-11-28 00:45:57 +01:00
|
|
|
if (this.inArray) {
|
2022-10-12 23:48:59 +02:00
|
|
|
return new VStringArray({ target: 'json', key: key })
|
|
|
|
} else {
|
|
|
|
return new VString({ target: 'json', key: key })
|
|
|
|
}
|
|
|
|
}
|
2022-11-28 00:45:57 +01:00
|
|
|
array = <T extends Schema>(path: string, validatorFn: (v: Validator) => T): VArray<T> => {
|
|
|
|
const validator = new Validator(true)
|
|
|
|
const res = validatorFn(validator)
|
2022-10-12 23:48:59 +02:00
|
|
|
const arr = new VArray(res, path)
|
|
|
|
return arr
|
|
|
|
}
|
2022-11-28 00:45:57 +01:00
|
|
|
object = <T extends Schema>(path: string, validatorFn: (v: Validator) => T): VObject<T> => {
|
|
|
|
const validator = new Validator(this.inArray)
|
|
|
|
const res = validatorFn(validator)
|
2022-10-12 23:48:59 +02:00
|
|
|
const obj = new VObject(res, path)
|
|
|
|
return obj
|
|
|
|
}
|
2022-09-13 00:54:06 +02:00
|
|
|
}
|
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
type VOptions = {
|
|
|
|
target: Target
|
|
|
|
key: string
|
|
|
|
type?: 'string' | 'number' | 'boolean' | 'object'
|
2022-09-30 14:56:39 +02:00
|
|
|
isArray?: boolean
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class VBase {
|
|
|
|
type: 'string' | 'number' | 'boolean' | 'object'
|
|
|
|
target: Target
|
2022-10-12 23:48:59 +02:00
|
|
|
baseKeys: string[] = []
|
2022-09-20 03:11:34 +02:00
|
|
|
key: string
|
|
|
|
rules: Rule[]
|
|
|
|
sanitizers: Sanitizer[]
|
2022-09-30 14:56:39 +02:00
|
|
|
isArray: boolean
|
2022-09-27 11:19:50 +02:00
|
|
|
private _optional: boolean
|
2022-09-20 03:11:34 +02:00
|
|
|
constructor(options: VOptions) {
|
|
|
|
this.target = options.target
|
|
|
|
this.key = options.key
|
|
|
|
this.type = options.type || 'string'
|
2022-10-19 12:17:04 +02:00
|
|
|
this.rules = [
|
|
|
|
{
|
|
|
|
name: this.getTypeRuleName(),
|
|
|
|
type: 'type',
|
|
|
|
func: this.validateType,
|
|
|
|
},
|
|
|
|
]
|
2022-09-20 03:11:34 +02:00
|
|
|
this.sanitizers = []
|
2022-09-27 11:19:50 +02:00
|
|
|
this._optional = false
|
2022-10-19 12:17:04 +02:00
|
|
|
this.isArray = options.isArray || false
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 23:48:59 +02:00
|
|
|
private _nested = () => (this.baseKeys.length ? true : false)
|
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
addRule(func: RuleFunc): this
|
|
|
|
addRule(name: string, func: RuleFunc): this
|
|
|
|
addRule(arg: string | RuleFunc, func?: RuleFunc) {
|
|
|
|
if (typeof arg === 'string' && func) {
|
|
|
|
this.rules.push({ name: arg, func, type: 'value' })
|
|
|
|
} else if (arg instanceof Function) {
|
|
|
|
this.rules.push({ name: arg.name, func: arg, type: 'value' })
|
|
|
|
}
|
2022-09-20 03:11:34 +02:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
addSanitizer = (sanitizer: Sanitizer) => {
|
|
|
|
this.sanitizers.push(sanitizer)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
message = (text: string) => {
|
|
|
|
const len = this.rules.length
|
|
|
|
if (len >= 1) {
|
|
|
|
this.rules[len - 1].customMessage = text
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
isRequired = () => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isRequired', (value: unknown) => {
|
2022-09-30 14:56:39 +02:00
|
|
|
if (value !== undefined && value !== null && value !== '') return true
|
2022-09-20 03:11:34 +02:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
isOptional = () => {
|
2022-09-27 11:19:50 +02:00
|
|
|
this._optional = true
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isOptional', () => true)
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isEqual = (comparison: unknown) => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isEqual', (value: unknown) => {
|
2022-09-20 03:11:34 +02:00
|
|
|
return value === comparison
|
|
|
|
})
|
|
|
|
}
|
2022-09-13 00:54:06 +02:00
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
asNumber = (): VNumber | VNumberArray => {
|
|
|
|
const newVNumber = new VNumber({ ...this, type: 'number' })
|
|
|
|
if (this.isArray) return newVNumber.asArray()
|
2022-09-20 03:11:34 +02:00
|
|
|
return newVNumber
|
|
|
|
}
|
2022-09-15 17:10:37 +02:00
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
asBoolean = (): VBoolean | VBooleanArray => {
|
|
|
|
const newVBoolean = new VBoolean({ ...this, type: 'boolean' })
|
|
|
|
if (this.isArray) return newVBoolean.asArray()
|
2022-09-20 03:11:34 +02:00
|
|
|
return newVBoolean
|
|
|
|
}
|
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
get(value: string) {
|
|
|
|
const len = this.rules.length
|
|
|
|
if (len > 0) {
|
|
|
|
this.rules[this.rules.length - 1].customMessage = value
|
|
|
|
}
|
2022-09-20 03:11:34 +02:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
2022-10-23 01:10:00 +02:00
|
|
|
validate = async <R extends Request>(req: R): Promise<ValidateResult[]> => {
|
2022-09-20 03:11:34 +02:00
|
|
|
let value: Type = undefined
|
2022-10-19 12:17:04 +02:00
|
|
|
let jsonData: JSONObject | undefined = undefined
|
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
if (this.target === 'query') {
|
|
|
|
value = req.query(this.key)
|
|
|
|
}
|
2022-11-01 06:22:04 +01:00
|
|
|
if (this.target === 'queries') {
|
|
|
|
value = req.queries(this.key)
|
|
|
|
}
|
2022-09-20 03:11:34 +02:00
|
|
|
if (this.target === 'header') {
|
|
|
|
value = req.header(this.key)
|
|
|
|
}
|
|
|
|
if (this.target === 'body') {
|
|
|
|
const body = await req.parseBody()
|
|
|
|
value = body[this.key]
|
|
|
|
}
|
|
|
|
if (this.target === 'json') {
|
2022-10-12 23:48:59 +02:00
|
|
|
if (this._nested()) {
|
|
|
|
this.key = `${this.baseKeys.join('.')}.${this.key}`
|
|
|
|
}
|
|
|
|
let obj = {}
|
2022-10-03 23:16:25 +02:00
|
|
|
try {
|
2022-10-12 23:48:59 +02:00
|
|
|
obj = (await req.json()) as JSONObject
|
2022-10-03 23:16:25 +02:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Malformed JSON in request body')
|
|
|
|
}
|
2022-10-12 23:48:59 +02:00
|
|
|
const dst = {}
|
|
|
|
value = JSONPathCopy(obj, dst, this.key)
|
2022-10-19 12:17:04 +02:00
|
|
|
if (this._nested()) jsonData = dst
|
|
|
|
}
|
|
|
|
|
2022-11-12 12:33:18 +01:00
|
|
|
value = this.sanitizeValue(value)
|
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
const results: ValidateResult[] = []
|
|
|
|
|
|
|
|
let typeRule = this.rules.shift()
|
|
|
|
|
|
|
|
for (const rule of this.rules) {
|
|
|
|
if (rule.type === 'type') {
|
|
|
|
typeRule = rule
|
|
|
|
} else if (rule.type === 'value') {
|
|
|
|
const result = this.validateRule(rule, value)
|
|
|
|
result.jsonData ||= jsonData
|
|
|
|
results.push(result)
|
2022-10-12 23:48:59 +02:00
|
|
|
}
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
if (typeRule) {
|
|
|
|
const typeResult = this.validateRule(typeRule, value)
|
|
|
|
typeResult.jsonData ||= jsonData
|
|
|
|
results.unshift(typeResult)
|
2022-10-23 03:11:18 +02:00
|
|
|
this.rules.unshift(typeRule)
|
2022-10-19 12:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getTypeRuleName(): string {
|
|
|
|
const prefix = 'should be'
|
|
|
|
return this.isArray ? `${prefix} "${this.type}[]"` : `${prefix} "${this.type}"`
|
|
|
|
}
|
|
|
|
|
2022-11-12 12:33:18 +01:00
|
|
|
private sanitizeValue = (value: Type) => (
|
|
|
|
this.sanitizers.reduce(
|
|
|
|
(acc, sanitizer) => sanitizer(acc),
|
|
|
|
value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
private validateRule(rule: Rule, value: Type): ValidateResult {
|
|
|
|
let isValid: boolean = false
|
2022-10-12 23:48:59 +02:00
|
|
|
|
|
|
|
if (this._nested() && this.target != 'json') {
|
2022-10-19 12:17:04 +02:00
|
|
|
isValid = false
|
|
|
|
} else if (rule.type === 'value') {
|
|
|
|
isValid = this.validateValue(rule.func, value)
|
|
|
|
} else if (rule.type === 'type') {
|
|
|
|
isValid = this.validateType(value)
|
2022-10-12 23:48:59 +02:00
|
|
|
}
|
2022-09-20 03:11:34 +02:00
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
const message = isValid
|
|
|
|
? undefined
|
|
|
|
: rule.customMessage || this.getMessage({ ruleName: rule.name, value })
|
|
|
|
const result = {
|
|
|
|
isValid: isValid,
|
|
|
|
message: message,
|
|
|
|
target: this.target,
|
|
|
|
key: this.key,
|
|
|
|
value,
|
|
|
|
ruleName: rule.name,
|
|
|
|
ruleType: rule.type,
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
2022-09-15 17:10:37 +02:00
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
private validateType = (value: Type): boolean => {
|
2022-09-30 14:56:39 +02:00
|
|
|
if (this.isArray) {
|
|
|
|
if (!Array.isArray(value)) {
|
2022-09-27 11:19:50 +02:00
|
|
|
return false
|
|
|
|
}
|
2022-09-30 14:56:39 +02:00
|
|
|
for (const val of value) {
|
2022-10-12 23:48:59 +02:00
|
|
|
if (typeof val === 'undefined' && this._nested()) {
|
|
|
|
value.pop()
|
|
|
|
}
|
|
|
|
for (const val of value) {
|
|
|
|
if (typeof val !== this.type) {
|
|
|
|
// Value is of wrong type here
|
2022-10-19 12:17:04 +02:00
|
|
|
// If it is not optional and not undefined, return false
|
2022-10-12 23:48:59 +02:00
|
|
|
if (!this._optional || typeof val !== 'undefined') return false
|
|
|
|
}
|
2022-09-30 14:56:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (typeof value !== this.type) {
|
2022-10-19 12:17:04 +02:00
|
|
|
if (this._optional && (typeof value === 'undefined' || Array.isArray(value))) {
|
2022-09-30 14:56:39 +02:00
|
|
|
// Do nothing.
|
2022-10-19 12:17:04 +02:00
|
|
|
// If it is optional it's OK to be `undefined` or Array
|
2022-09-30 14:56:39 +02:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-10-19 12:17:04 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2022-09-30 14:56:39 +02:00
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
private validateValue = (func: (value: Type) => boolean, value: Type): boolean => {
|
2022-10-20 09:19:13 +02:00
|
|
|
if (this._optional && typeof value === 'undefined') return true
|
|
|
|
|
2022-10-19 12:17:04 +02:00
|
|
|
if (Array.isArray(value)) {
|
2022-11-01 06:22:04 +01:00
|
|
|
if (value.length === 0 && !this._optional) return false
|
2022-10-19 12:17:04 +02:00
|
|
|
for (const val of value) {
|
|
|
|
if (!func(val)) {
|
2022-09-30 14:56:39 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2022-10-19 12:17:04 +02:00
|
|
|
} else {
|
|
|
|
if (!func(value)) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private getMessage = (opts: { ruleName: string; value: Type }): string => {
|
|
|
|
let keyText: string
|
|
|
|
const valueText = Array.isArray(opts.value)
|
|
|
|
? `${opts.value
|
|
|
|
.map((val) =>
|
|
|
|
val === undefined ? 'undefined' : typeof val === 'string' ? `"${val}"` : val
|
|
|
|
)
|
|
|
|
.join(', ')}`
|
|
|
|
: opts.value
|
|
|
|
switch (this.target) {
|
|
|
|
case 'query':
|
|
|
|
keyText = `the query parameter "${this.key}"`
|
|
|
|
break
|
2022-11-01 06:22:04 +01:00
|
|
|
case 'queries':
|
|
|
|
keyText = `the query parameters "${this.key}"`
|
|
|
|
break
|
2022-10-19 12:17:04 +02:00
|
|
|
case 'header':
|
|
|
|
keyText = `the request header "${this.key}"`
|
|
|
|
break
|
|
|
|
case 'body':
|
|
|
|
keyText = `the request body "${this.key}"`
|
|
|
|
break
|
|
|
|
case 'json':
|
|
|
|
keyText = `the JSON body "${this.key}"`
|
|
|
|
break
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
2022-10-19 12:17:04 +02:00
|
|
|
return `Invalid Value [${valueText}]: ${keyText} is invalid - ${opts.ruleName}`
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-13 00:54:06 +02:00
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
export class VString extends VBase {
|
|
|
|
constructor(options: VOptions) {
|
|
|
|
super(options)
|
|
|
|
this.type = 'string'
|
|
|
|
}
|
2022-09-13 00:54:06 +02:00
|
|
|
|
2022-09-30 14:56:39 +02:00
|
|
|
asArray = () => {
|
|
|
|
return new VStringArray(this)
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
isEmpty = (
|
2022-09-13 00:54:06 +02:00
|
|
|
options: {
|
|
|
|
ignore_whitespace: boolean
|
|
|
|
} = { ignore_whitespace: false }
|
2022-09-20 03:11:34 +02:00
|
|
|
) => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isEmpty', (value) => rule.isEmpty(value as string, options))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isLength = (options: Partial<{ min: number; max: number }> | number, arg2?: number) => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isLength', (value) => rule.isLength(value as string, options, arg2))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isAlpha = () => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isAlpha', (value) => rule.isAlpha(value as string))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isNumeric = () => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isNumeric', (value) => rule.isNumeric(value as string))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
contains = (
|
2022-09-13 00:54:06 +02:00
|
|
|
elem: string,
|
|
|
|
options: Partial<{ ignoreCase: boolean; minOccurrences: number }> = {
|
|
|
|
ignoreCase: false,
|
|
|
|
minOccurrences: 1,
|
|
|
|
}
|
2022-09-20 03:11:34 +02:00
|
|
|
) => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('contains', (value) => rule.contains(value as string, elem, options))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isIn = (options: string[]) => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isIn', (value) => rule.isIn(value as string, options))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
match = (regExp: RegExp) => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('match', (value) => rule.match(value as string, regExp))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
trim = () => {
|
|
|
|
return this.addSanitizer((value) => sanitizer.trim(value as string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class VNumber extends VBase {
|
|
|
|
constructor(options: VOptions) {
|
|
|
|
super(options)
|
|
|
|
this.type = 'number'
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:56:39 +02:00
|
|
|
asArray = () => {
|
|
|
|
return new VNumberArray(this)
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
isGte = (min: number) => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isGte', (value) => rule.isGte(value as number, min))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isLte = (min: number) => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isLte', (value) => rule.isLte(value as number, min))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class VBoolean extends VBase {
|
|
|
|
constructor(options: VOptions) {
|
|
|
|
super(options)
|
|
|
|
this.type = 'boolean'
|
|
|
|
}
|
|
|
|
|
2022-09-30 14:56:39 +02:00
|
|
|
asArray = () => {
|
|
|
|
return new VBooleanArray(this)
|
|
|
|
}
|
|
|
|
|
2022-09-20 03:11:34 +02:00
|
|
|
isTrue = () => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isTrue', (value) => rule.isTrue(value as boolean))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isFalse = () => {
|
2022-10-19 12:17:04 +02:00
|
|
|
return this.addRule('isFalse', (value) => rule.isFalse(value as boolean))
|
2022-09-20 03:11:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-09-13 00:54:06 +02:00
|
|
|
|
2022-09-30 14:56:39 +02:00
|
|
|
export class VNumberArray extends VNumber {
|
|
|
|
isArray: true
|
|
|
|
constructor(options: VOptions) {
|
|
|
|
super(options)
|
|
|
|
this.isArray = true
|
2022-10-19 12:17:04 +02:00
|
|
|
this.rules[0].name = this.getTypeRuleName()
|
2022-09-30 14:56:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
export class VStringArray extends VString {
|
|
|
|
isArray: true
|
|
|
|
constructor(options: VOptions) {
|
|
|
|
super(options)
|
|
|
|
this.isArray = true
|
2022-10-19 12:17:04 +02:00
|
|
|
this.rules[0].name = this.getTypeRuleName()
|
2022-09-30 14:56:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
export class VBooleanArray extends VBoolean {
|
|
|
|
isArray: true
|
|
|
|
constructor(options: VOptions) {
|
|
|
|
super(options)
|
|
|
|
this.isArray = true
|
2022-10-19 12:17:04 +02:00
|
|
|
this.rules[0].name = this.getTypeRuleName()
|
2022-09-30 14:56:39 +02:00
|
|
|
}
|
|
|
|
}
|