Skip to content

Commit

Permalink
Added typescript typings in index.d.ts. (#16)
Browse files Browse the repository at this point in the history
* Added typescript typings in index.d.ts
  • Loading branch information
DJWassink authored and nettofarah committed Apr 11, 2017
1 parent af15aee commit 352fa2c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
100 changes: 100 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/// <reference types="validator" />

declare module 'property-validator' {

//middleware
export function assertMiddleware(err: Error, req: {}, res: {}, next: Function): void;

//result of a validation/assertion
export type ValidateResult = {
valid: boolean;
errors: {
field: string;
message: string;
};
messages: string[]
};

//error type
export class ValidationError extends Error {
name: 'ValidationError';
message: string;
messages: string[];
stack: string;
errors: Array<{
field: string;
message: string;
}>;
}

//type of a validation function, typically a wrapper around Validator.js
export type Validator = (subject: {}) => ValidatorResult;

//result of a validation function
export type ValidatorResult = {
field: string;
message: string;
result: boolean;
}

//request validators
export function validate(subject: {}, string[], validations: Validator[]): ValidateResult;
export function validateParams(request: { params: {} }, validations: Validator[]): ValidateResult;
export function validateQuery(request: { query: {} }, validations: Validator[]): ValidateResult;
export function validateBody(request: { body: {} }, validations: Validator[]): ValidateResult;
export function validateHeaders(request: { headers: {} }, validations: Validator[]): ValidateResult;
export function validateAll(request: { query?: {}, params?: {}, body?: {} }, validations: Validator[]): ValidateResult;

//request assertions
export function assert(subject: {}, validations: Validator[]): ValidateResult;
export function assertHeaders(request: { headers: {} }, validations: Validator[]): ValidateResult;
export function assertParams(request: { params: {} }, validations: Validator[]): ValidateResult;
export function assertBody(request: { body: {} }, validations: Validator[]): ValidateResult;
export function assertQuery(request: { query: {} }, validations: Validator[]): ValidateResult;
export function assertAll(request: { query?: {}, params?: {}, body?: {} }, validations: Validator[]): ValidateResult;

//special assert which makes a prop optional
export function optional(validation: Validator, customMessage?: string): Validator;

//validation functions
export function presence(paramName: string, customMessage?: string): Validator;
export function contains(paramName: string, str: string, customMessage?: string): Validator;
export function isAlpha(paramName: string, customMessage?: string): Validator;
export function isAlphanumeric(paramName: string, customMessage?: string): Validator;
export function isArray(paramName: string, customMessage?: string): Validator;
export function isCreditCard(paramName: string, customMessage?: string): Validator;
export function isCurrency(paramName: string, options: ValidatorJS.IsCurrencyOptions, customMessage?: string): Validator;
export function isDate(paramName: string, customMessage?: string): Validator;
export function isDecimal(paramName: string, customMessage?: string): Validator;
export function isInt(paramName: string, options: ValidatorJS.IsIntOptions, customMessage?: string): Validator;
export function isJSON(paramName: string, customMessage?: string): Validator;
export function isNull(paramName: string, customMessage?: string): Validator;
export function isNumeric(paramName: string, customMessage?: string): Validator;
export function isURL(paramName: string, options: ValidatorJS.IsURLOptions, customMessage?: string): Validator;
export function isPlainObject(paramName: string, customMessage?: string): Validator;

//same named (pair) validation functions
export function email(paramName: string, customMessage?: string): Validator;
export function isEmail(paramName: string, customMessage?: string): Validator;
export function equals(paramName: string, comparison: string, customMessage?: string): Validator;
export function isEqual(paramName: string, comparison: string, customMessage?: string): Validator;
export function matches(paramName: string, pattern: RegExp | string, customMessage?: string): Validator;
export function format(paramName: string, pattern: RegExp | string, customMessage?: string): Validator;
export function uuid(paramName: string, version?: string, customMessage?: string): Validator;
export function isUUID(paramName: string, version?: string, customMessage?: string): Validator;
export function isIn(paramName: string, values: any[], customMessage?: string): Validator;
export function oneOf(paramName: string, values: any[], customMessage?: string): Validator;
export function isLength(paramName: string, options: IsLengthOptions, customMessage?: string): Validator;
export function length(paramName: string, options: IsLengthOptions, customMessage?: string): Validator;

//export ValidatorJS options interfaces
export interface IsByteLengthOptions extends ValidatorJS.IsByteLengthOptions{}
export interface IsCurrencyOptions extends ValidatorJS.IsCurrencyOptions{}
export interface IsEmailOptions extends ValidatorJS.IsEmailOptions{}
export interface IsFQDNOptions extends ValidatorJS.IsFQDNOptions{}
export interface IsFloatOptions extends ValidatorJS.IsFloatOptions{}
export interface IsIntOptions extends ValidatorJS.IsIntOptions{}
export interface IsLengthOptions extends ValidatorJS.IsLengthOptions{}
export interface IsURLOptions extends ValidatorJS.IsURLOptions{}
export interface NormalizeEmailOptions extends ValidatorJS.NormalizeEmailOptions{}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.7.0",
"description": "Easy property validation for JavaScript, Node and Express.",
"main": "index.js",
"types": "./index.d.ts",
"homepage": "http://github.com/nettofarah/property-validator",
"scripts": {
"test": "./node_modules/mocha/bin/mocha test/"
Expand Down

0 comments on commit 352fa2c

Please sign in to comment.