Merge pull request #5670 from psnider/tv4

updated tv4 to fully match 1.2.5
This commit is contained in:
Masahiro Wakame
2015-09-08 23:32:12 +09:00
4 changed files with 336 additions and 59 deletions
+43
View File
@@ -0,0 +1,43 @@
///<reference path="tv4-1.2.4.d.ts" />
var str:string;
var strArr:string[];
var bool:boolean;
var num:number;
var obj:any;
var tv4:TV4;
var err:TV4Error;
var errs:TV4Error[];
var single:TV4SingleResult;
var multi:TV4MultiResult;
single = tv4.validateResult(obj, obj);
bool = single.valid;
strArr = single.missing;
err = single.error;
num = err.code;
str = err.message;
str = err.dataPath;
str = err.schemaPath;
multi = tv4.validateMultiple(obj, obj);
bool = multi.valid;
strArr = multi.missing;
errs = multi.errors;
bool = tv4.addSchema(str, obj);
obj = tv4.getSchema(str);
obj = tv4.normSchema(str, str);
str = tv4.resolveUrl(str, str);
tv4 = tv4.freshApi();
tv4.dropSchemas();
tv4.reset();
strArr = tv4.getMissingUris(/abc/);
strArr = tv4.getSchemaUris(/abc/);
obj = tv4.getSchemaMap()[str];
num = tv4.errorCodes['bla'];
num = tv4.errorCodes['MY_NAME'];
+48
View File
@@ -0,0 +1,48 @@
// Type definitions for Tiny Validator tv4 1.0.6
// Project: https://github.com/geraintluff/tv4
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
interface TV4ErrorCodes {
[key:string]:number;
}
interface TV4Error {
code:number;
message:string;
dataPath:string;
schemaPath:string;
}
interface TV4SchemaMap {
[uri:string]:any;
}
interface TV4BaseResult {
missing:string[];
valid:boolean;
}
interface TV4SingleResult extends TV4BaseResult {
error:TV4Error;
}
interface TV4MultiResult extends TV4BaseResult {
errors:TV4Error[];
}
interface TV4 {
validateResult(data:any, schema:any):TV4SingleResult;
validateMultiple(data:any, schema:any):TV4MultiResult;
addSchema(uri:string, schema:any):boolean;
getSchema(uri:string):any;
normSchema(schema:any, baseUri:string):any;
resolveUrl(base:string, href:string):string;
freshApi():TV4;
dropSchemas():void;
reset():void;
getMissingUris(exp?:RegExp):string[];
getSchemaUris(exp?:RegExp):string[];
getSchemaMap():TV4SchemaMap;
errorCodes:TV4ErrorCodes;
}
declare module "tv4" {
var tv4: TV4
export = tv4;
}
+150 -19
View File
@@ -5,13 +5,13 @@ var strArr:string[];
var bool:boolean;
var num:number;
var obj:any;
var tv4:TV4;
var err:TV4Error;
var errs:TV4Error[];
var single:TV4SingleResult;
var multi:TV4MultiResult;
var validator: tv4.TV4;
var err:tv4.ValidationError;
var errs:tv4.ValidationError[];
var single:tv4.SingleResult;
var multi:tv4.MultiResult;
single = tv4.validateResult(obj, obj);
single = validator.validateResult(obj, obj);
bool = single.valid;
strArr = single.missing;
err = single.error;
@@ -21,23 +21,154 @@ str = err.message;
str = err.dataPath;
str = err.schemaPath;
multi = tv4.validateMultiple(obj, obj);
multi = validator.validateMultiple(obj, obj);
bool = multi.valid;
strArr = multi.missing;
errs = multi.errors;
bool = tv4.addSchema(str, obj);
obj = tv4.getSchema(str);
obj = tv4.normSchema(str, str);
str = tv4.resolveUrl(str, str);
validator.addSchema(str, obj);
obj = validator.getSchema(str);
str = validator.resolveUrl(str, str);
validator = validator.freshApi();
validator.dropSchemas();
validator.reset();
strArr = validator.getMissingUris(/abc/);
strArr = validator.getSchemaUris(/abc/);
obj = validator.getSchemaMap()[str];
num = validator.errorCodes['bla'];
num = validator.errorCodes['MY_NAME'];
// Here are all the examples from the v1.2.3 documentation at https://www.npmjs.com/package/validator
var data = '';
var schema : tv4.JsonSchema = {type: "string"}
var valid = validator.validate(data, schema);
var url = 'http://example.com/schema';
validator.addSchema(url, schema);
var singleErrorResult = validator.validateResult(data, schema);
var multiErrorResult = validator.validateMultiple(data, schema);
// async
validator.validate(data, schema, function (isValid, validationError) {});
// checkRecursive
var a : tv4.JsonSchema = {};
var b = { a: a };
a['b'] = b;
var aSchema : tv4.JsonSchema = { properties: { b: { $ref: 'bSchema' }}};
var bSchema : tv4.JsonSchema = { properties: { a: { $ref: 'aSchema' }}};
validator.addSchema('aSchema', aSchema);
validator.addSchema('bSchema', bSchema);
validator.validate(a, aSchema, true);
validator.validateResult(data, aSchema, true);
validator.validateMultiple(data, aSchema, true);
// banUnknownProperties
var checkRecursive = true;
validator.validate(data, schema, checkRecursive, true);
validator.validateResult(data, schema, checkRecursive, true);
validator.validateMultiple(data, schema, checkRecursive, true);
// API
validator.addSchema('http://example.com/schema', {});
validator.addSchema({});
var schema = validator.getSchema('http://example.com/schema');
var map = validator.getSchemaMap();
var schema = map[uri];
var arr = validator.getSchemaUris();
// optional filter using a RegExp
arr = validator.getSchemaUris(/^https?:\/\/example.com/);
var arr = validator.getMissingUris();
// optional filter using a RegExp
var arr = validator.getMissingUris(/^https?:\/\/example.com/);
validator.dropSchemas();
var other_tv4 = validator.freshApi();
validator.reset();
validator.setErrorReporter(function (error, data, schema) {
return "Error code: " + error.code;
});
validator.language('en-gb');
validator.addLanguage('fr', {});
validator.language('fr')
validator.addFormat('decimal-digits', function (data, schema) {
if (typeof data === 'string' && !/^[0-9]+$/.test(data)) {
return null;
}
return "must be string of decimal digits";
});
validator.addFormat({
'my-format': function (data: any, schema: any): string {return null;},
'other-format': function (data: any, schema: any): string {return 'oops';}
});
function simpleFailure() {return true;}
function detailedFailure() {return true;}
validator.defineKeyword('my-custom-keyword', function (data, value, schema) {
if (simpleFailure()) {
return "Failure";
} else if (detailedFailure()) {
return {code: validator.errorCodes['MY_CUSTOM_CODE'], message: {param1: 'a', param2: 'b'}};
} else {
return null;
}
});
// Demos
schema = {
"items": {
"type": "boolean"
}
};
{
let data1 = [true, false];
let data2 = [true, 123];
alert("data 1: " + validator.validate(data1, schema)); // true
alert("data 2: " + validator.validate(data2, schema)); // false
alert("data 2 error: " + JSON.stringify(validator.error, null, 4));
schema = {
"type": "array",
"items": {"$ref": "#"}
};
}
{
let data1 : any = [[], [[]]];
let data2 : any = [[], [true, []]];
alert("data 1: " + validator.validate(data1, schema)); // true
alert("data 2: " + validator.validate(data2, schema)); // false
}
{
schema = {
"type": "array",
"items": {"$ref": "http://example.com/schema" }
};
let data = [1, 2, 3];
alert("Valid: " + validator.validate(data, schema)); // true
alert("Missing schemas: " + JSON.stringify(validator.missing));
}
{
validator.addSchema("http://example.com/schema", {
"definitions": {
"arrayItem": {"type": "boolean"}
}
});
let schema : tv4.JsonSchema = {
"type": "array",
"items": {"$ref": "http://example.com/schema#/definitions/arrayItem" }
};
let data1 : any = [true, false, true];
let data2 : any = [1, 2, 3];
alert("data 1: " + validator.validate(data1, schema)); // true
alert("data 2: " + validator.validate(data2, schema)); // false
}
// undocumented functions
var uri = '';
obj = validator.normSchema(schema, uri);
tv4 = tv4.freshApi();
tv4.dropSchemas();
tv4.reset();
strArr = tv4.getMissingUris(/abc/);
strArr = tv4.getSchemaUris(/abc/);
obj = tv4.getSchemaMap()[str];
num = tv4.errorCodes['bla'];
num = tv4.errorCodes['MY_NAME'];
+95 -40
View File
@@ -1,48 +1,103 @@
// Type definitions for Tiny Validator tv4 1.0.6
// Type definitions for Tiny Validator tv4 1.2.5
// Project: https://github.com/geraintluff/tv4
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
// Definitions by: Bart van der Schoor <https://github.com/Bartvds>, Peter Snider <https://github.com/psnider>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
interface TV4ErrorCodes {
[key:string]:number;
}
interface TV4Error {
code:number;
message:string;
dataPath:string;
schemaPath:string;
}
interface TV4SchemaMap {
[uri:string]:any;
}
interface TV4BaseResult {
missing:string[];
valid:boolean;
}
interface TV4SingleResult extends TV4BaseResult {
error:TV4Error;
}
interface TV4MultiResult extends TV4BaseResult {
errors:TV4Error[];
}
interface TV4 {
validateResult(data:any, schema:any):TV4SingleResult;
validateMultiple(data:any, schema:any):TV4MultiResult;
declare module tv4 {
// Note that every top-level property is optional in json-schema
export interface JsonSchema {
[key: string]: any;
title?: string; // used for humans only, and not used for computation
description?: string; // used for humans only, and not used for computation
id?: string;
$schema?: string;
type?: string;
items?: any;
properties?: any;
patternProperties?: any;
additionalProperties?: boolean;
required?: string[];
definitions?: any;
default?: any;
}
addSchema(uri:string, schema:any):boolean;
getSchema(uri:string):any;
normSchema(schema:any, baseUri:string):any;
resolveUrl(base:string, href:string):string;
freshApi():TV4;
dropSchemas():void;
reset():void;
export type SchemaMap = {[uri: string]: JsonSchema;};
// maps error codes/names to human readable error description for a single language
export type ErrorMap = {[errorCode: string]: string;};
getMissingUris(exp?:RegExp):string[];
getSchemaUris(exp?:RegExp):string[];
getSchemaMap():TV4SchemaMap;
errorCodes:TV4ErrorCodes;
export interface ErrorCodes {
[key:string]:number;
}
export interface ValidationError {
code:number;
message:any;
dataPath?:string;
schemaPath?:string;
subErrors?: ValidationError[];
}
export interface ErrorVar extends ValidationError {
params: any;
subErrors: any;
stack: string;
}
export interface BaseResult {
missing:string[];
valid:boolean;
}
export interface SingleResult extends BaseResult {
error:ValidationError;
}
export interface MultiResult extends BaseResult {
errors:ValidationError[];
}
export type FormatValidationFunction = (data: any, schema: JsonSchema) => string;
// documentation doesnt agree with code in tv4, this type agrees with code
export type KeywordValidationFunction = (data: any, value: any, schema: JsonSchema, dataPointerPath: string) => string | ValidationError;
export type AsyncValidationCallback = (isValid: boolean, error: ValidationError) => void;
export interface TV4 {
error: ErrorVar;
missing: string[];
// primary API
validate(data: any, schema: JsonSchema, checkRecursive?: boolean): boolean;
validate(data: any, schema: JsonSchema, checkRecursive: boolean, banUnknownProperties: boolean): boolean;
validateResult(data: any, schema: JsonSchema, checkRecursive?: boolean): SingleResult;
validateResult(data: any, schema: JsonSchema, checkRecursive: boolean, banUnknownProperties: boolean): SingleResult;
validateMultiple(data: any, schema: JsonSchema, checkRecursive?: boolean): MultiResult;
validateMultiple(data: any, schema: JsonSchema, checkRecursive: boolean, banUnknownProperties: boolean): MultiResult;
// from including: tv4.async-jquery.js
validate(data: any, schema: JsonSchema, callback: AsyncValidationCallback, checkRecursive?: boolean): void;
validate(data: any, schema: JsonSchema, callback: AsyncValidationCallback, checkRecursive: boolean, banUnknownProperties: boolean): void;
// additional API for more complex cases
addSchema(schema: JsonSchema): void;
addSchema(uri:string, schema: JsonSchema): void;
getSchema(uri:string): JsonSchema;
getSchemaMap(): SchemaMap;
getSchemaUris(filter?: RegExp): string[];
getMissingUris(filter?: RegExp): string[];
dropSchemas(): void;
freshApi(): TV4;
reset(): void;
setErrorReporter(lang: string): void;
setErrorReporter(reporter: (error: ValidationError, data: any, schema: JsonSchema) => string): void;
language(code: string): void;
addLanguage(code: string, map: ErrorMap): void;
addFormat(format: string, validationFunction: FormatValidationFunction): void;
addFormat(formats: {[formatName: string]: FormatValidationFunction;}): void;
defineKeyword(keyword: string, validationFunction: KeywordValidationFunction): void;
defineError(codeName: string, codeNumber: number, defaultMessage: string): void;
// not documented
normSchema(schema: JsonSchema, baseUri:string):any;
resolveUrl(base:string, href:string):string;
errorCodes:ErrorCodes;
}
}
declare module "tv4" {
var tv4: TV4
export = tv4;
var out: tv4.TV4
export = out;
}