Files
freeplanet/src/typings/libs/vuelidate.d.ts

250 lines
7.3 KiB
TypeScript
Raw Normal View History

declare module 'vuelidate' {
2018-11-11 19:27:04 +01:00
import _Vue = require('vue')
/**
* @module augmentation to ComponentOptions defined by Vue.js
2018-11-11 19:27:04 +01:00
*/
module 'vue/types/options' {
2018-11-11 19:27:04 +01:00
interface ComponentOptions<V extends _Vue> {
validations?: ValidationRuleset<{}>
}
}
module 'vue/types/vue' {
interface Vue {
$v: Vuelidate<any>
}
}
/**
* Represents an instance of validator class at runtime
2018-11-11 19:27:04 +01:00
*/
export interface IValidator {
/**
* Indicates the state of validation for given model. becomes true when any of it's child validators specified in options returns a falsy value. In case of validation groups, all grouped validators are considered.
2018-11-11 19:27:04 +01:00
*/
readonly $invalid: boolean
/**
* A flag representing if the field under validation was touched by the user at least once. Usually it is used to decide if the message is supposed to be displayed to the end user. Flag is managed manually. You have to use $touch and $reset methods to manipulate it. The $dirty flag is considered true if given model was $touched or all of it's children are $dirty.
*/
$dirty: boolean
/**
* Convenience flag to easily decide if a message should be displayed. It is a shorthand to $invalid && $dirty.
*/
readonly $error: boolean
/**
* Indicates if any child async validator is currently pending. Always false if all validators are synchronous.
*/
$pending: boolean
2018-11-11 19:27:04 +01:00
$params: any
/**
* Sets the $dirty flag of the model and all its children to true recursively.
*/
$touch(): void
/**
* Sets the $dirty flag of the model and all its children to false recursively.
*/
$reset(): void
2018-11-11 19:27:04 +01:00
$flattenParams(): void
}
/**
* Builtin validators
2018-11-11 19:27:04 +01:00
*/
interface IDefaultValidators {
/**
2018-11-11 19:27:04 +01:00
* Accepts only alphabet characters.
*/
alpha?: boolean
/**
2018-11-11 19:27:04 +01:00
* Accepts only alphanumerics.
*/
alphaNum?: boolean
/**
2018-11-11 19:27:04 +01:00
* Checks if a number is in specified bounds. Min and max are both inclusive.
*/
between?: boolean
/**
2018-11-11 19:27:04 +01:00
* Accepts valid email addresses. Keep in mind you still have to carefully verify it on your server, as it is impossible to tell if the address is real without sending verification email.
*/
email?: boolean
/**
2018-11-11 19:27:04 +01:00
* Requires the input to have a maximum specified length, inclusive. Works with arrays.
*/
maxLength?: boolean
/**
2018-11-11 19:27:04 +01:00
* Requires the input to have a minimum specified length, inclusive. Works with arrays.
*/
minLength?: boolean
/**
2018-11-11 19:27:04 +01:00
* Requires non-empty data. Checks for empty arrays and strings containing only whitespaces.
*/
required?: boolean
/**
2018-11-11 19:27:04 +01:00
* Checks for equality with a given property. Locator might be either a sibling property name or a function, that will get your component as this and nested model which sibling properties under second parameter.
*/
sameAs?: boolean
/**
2018-11-11 19:27:04 +01:00
* Passes when at least one of provided validators passes.
*/
or?: boolean
/**
2018-11-11 19:27:04 +01:00
* Passes when all of provided validators passes.
*/
and?: boolean
}
type EachByKey<T> = {
[K in keyof T]: Validator<T[K]>
}
/**
* Holds all validation models of collection validator. Always preserves the keys of original model, so it can be safely referenced in the v-for loop iterating over your data using the same index.
2018-11-11 19:27:04 +01:00
*/
type Each<T> =
2018-11-11 19:27:04 +01:00
& { [key: number]: EachByKey<T> }
& { $trackBy: string | Function }
& IValidator
global {
interface Array<T> {
/**
* Holds all validation models of collection validator. Always preserves the keys of original model, so it can be safely referenced in the v-for loop iterating over your data using the same index.
2018-11-11 19:27:04 +01:00
*/
$each: Each<T> & Vuelidate<T>
}
}
/**
2018-11-11 19:27:04 +01:00
* Represents an instance of validator class at runtime
*/
type Validator<T> = IValidator & IDefaultValidators & Each<T>
interface IPredicate {
(value: any, parentVm?: IValidationRule): boolean | Promise<boolean>
}
interface IPredicateGenerator {
(...args: any[]): IPredicate
}
interface IValidationRule {
[key: string]: ValidationPredicate | IValidationRule | IValidationRule[]
}
export type ValidationPredicate = IPredicateGenerator | IPredicate
/**
* Represents mixin data exposed by Vuelidate instance
2018-11-11 19:27:04 +01:00
*/
export type Vuelidate<T> = {
[K in keyof T]?: Vuelidate<T[K]> & Validator<T[K]>;
}
/**
* Represents component options used by Vuelidate
2018-11-11 19:27:04 +01:00
*/
export type ValidationRuleset<T> = {
[K in keyof T]?: ValidationPredicate | IValidationRule | IValidationRule[] | string[];
}
/**
* Represents Vuelidate mixin data extending a Vue component instance. Have your Vue component options implement this
* @param {Type} T - The interface or type being used to store model data requiring validation
2018-11-11 19:27:04 +01:00
*
* @example
* export class Foo implements IVuelidate<IBar> {
* data() {
* return { bar: { length: 0 } };
* }
* validations: {
* bar: {
* length: {
* between: between(1,5)
* }
* }
* }
* $v: Vuelidate<IBar>;
* }
2018-11-11 19:27:04 +01:00
*/
export interface IVuelidate<T> {
$v: Vuelidate<T>
}
/**
* Mixin object for supplying directly to components
2018-11-11 19:27:04 +01:00
*/
export const validationMixin: {
beforeCreate(): void;
}
/**
* Vuelidate function that creates a validator directly, given a model, and a set of rules
2018-11-11 19:27:04 +01:00
*/
export const validateModel: {
<T>(model: T, validations: ValidationRuleset<T>): IVuelidate<T>;
}
/**
* Vue plugin object
2018-11-11 19:27:04 +01:00
*/
export function Validation(Vue: typeof _Vue): void
export default Validation
}
declare module 'vuelidate/lib/validators' {
import { ValidationPredicate } from 'vuelidate'
/**
* Accepts only alphabet characters.
*/
function alpha(value: any): boolean
/**
* Accepts only alphanumerics.
*/
function alphaNum(value: any): boolean
/**
* Checks if a number is in specified bounds. Min and max are both inclusive.
*/
function between(min: number, max: number): (value: any) => boolean
/**
* Accepts valid email addresses. Keep in mind you still have to carefully verify it on your server, as it is impossible to tell if the address is real without sending verification email.
2018-11-11 19:27:04 +01:00
*/
function email(value: any): boolean
/**
* Requires the input to have a maximum specified length, inclusive. Works with arrays.
2018-11-11 19:27:04 +01:00
*/
function maxLength(max: number): (value: any) => boolean
/**
* Requires the input to have a minimum specified length, inclusive. Works with arrays.
*/
function minLength(min: number): (value: any) => boolean
/**
* Requires non-empty data. Checks for empty arrays and strings containing only whitespaces.
2018-11-11 19:27:04 +01:00
*/
function required(value: any): boolean
/**
* Checks for equality with a given property. Locator might be either a sibling property name or a function, that will get your component as this and nested model which sibling properties under second parameter.
2018-11-11 19:27:04 +01:00
*/
function sameAs(locator: any): (value: any, vm?: any) => boolean
/**
* Passes when at least one of provided validators passes.
*/
function or(...validators: ValidationPredicate[]): () => boolean
/**
* Passes when all of provided validators passes.
*/
function and(...validators: ValidationPredicate[]): () => boolean
2018-11-11 19:27:04 +01:00
function numeric()
function minValue(value: number)
function maxValue(value: number)
}