mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-08-27 11:40:08 +08:00
@@ -0,0 +1,69 @@
|
||||
/// <reference path="jjv.d.ts" />
|
||||
|
||||
import jjv = require('jjv');
|
||||
|
||||
// create new JJV environment
|
||||
var env = jjv();
|
||||
var errors: jjv.Errors;
|
||||
|
||||
// Register a `user` schema
|
||||
env.addSchema('user', {
|
||||
type: 'object',
|
||||
properties: {
|
||||
firstname: {
|
||||
type: 'string',
|
||||
minLength: 2,
|
||||
maxLength: 15,
|
||||
},
|
||||
lastname: {
|
||||
type: 'string',
|
||||
minLength: 2,
|
||||
maxLength: 25,
|
||||
},
|
||||
gender: {
|
||||
type: 'string',
|
||||
enum: ['male', 'female'],
|
||||
},
|
||||
email: {
|
||||
type: 'string',
|
||||
format: 'email',
|
||||
},
|
||||
password: {
|
||||
type: 'string',
|
||||
minLength: 8,
|
||||
},
|
||||
},
|
||||
required: ['firstname', 'lastname', 'email', 'password'],
|
||||
});
|
||||
|
||||
// Perform validation against an incomplete user object (errors will be reported)
|
||||
errors = env.validate('user', { firstname: 'John', lastname: 'Smith' });
|
||||
|
||||
errors = env.validate({
|
||||
type: 'object',
|
||||
properties: {
|
||||
x: { type: 'number' },
|
||||
y: { type: 'number' },
|
||||
},
|
||||
required: ['x', 'y'],
|
||||
}, { x: 'a' });
|
||||
|
||||
if (errors.validation['x'].type === 'string') {
|
||||
console.log('x is wrong type');
|
||||
}
|
||||
|
||||
if (errors.validation['y'].required) {
|
||||
console.log('y is required');
|
||||
}
|
||||
|
||||
env.defaultOptions.checkRequired = false;
|
||||
|
||||
env.validate('schemaName', {}, { checkRequired: false });
|
||||
|
||||
env.addType('date', (v: any) => !isNaN(Date.parse(v)));
|
||||
|
||||
env.addFormat('hexadecimal', (v: any) => (/^[a-fA-F0-9]+$/).test(v));
|
||||
|
||||
env.addCheck('exactLength', (v: any, p: any) => v.length === p);
|
||||
|
||||
env.addTypeCoercion('integer', (x: any) => parseInt(x, 10));
|
||||
Vendored
+42
@@ -0,0 +1,42 @@
|
||||
// Type definitions for JJV v1.0.2
|
||||
// Project: https://github.com/acornejo/jjv
|
||||
// Definitions by: Wim Looman <https://github.com/Nemo157>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
declare module "jjv" {
|
||||
function jjv(): jjv.Env;
|
||||
|
||||
module jjv {
|
||||
interface Errors {
|
||||
validation: {
|
||||
[property: string]: {
|
||||
required?: boolean;
|
||||
type?: string;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
interface Options {
|
||||
checkRequired?: boolean;
|
||||
useDefault?: boolean;
|
||||
useCoerce?: boolean;
|
||||
removeAdditional?: boolean;
|
||||
}
|
||||
|
||||
interface Env {
|
||||
defaultOptions: Options;
|
||||
|
||||
addSchema(name: string, schema: Object): void;
|
||||
|
||||
addType(name: string, parse: (input: any) => any): void;
|
||||
addFormat(name: string, parse: (input: any) => any): void;
|
||||
addCheck(name: string, check: (input: any, comparator: any) => any): void;
|
||||
addTypeCoercion(name: string, coerce: (input: any) => any): void;
|
||||
|
||||
validate(name: string, object: any, options?: Options): Errors;
|
||||
validate(schema: Object, object: any, options?: Options): Errors;
|
||||
}
|
||||
}
|
||||
|
||||
export = jjv;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/// <reference path="../jjv/jjv.d.ts" />
|
||||
/// <reference path="jjve.d.ts" />
|
||||
|
||||
import jjv = require('jjv');
|
||||
import jjve = require('jjve');
|
||||
|
||||
var env: jjv.Env = jjv();
|
||||
var je: jjve.Env = jjve(env);
|
||||
|
||||
var schema = {
|
||||
type: 'object',
|
||||
properties: {
|
||||
ok: {
|
||||
type: 'boolean',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
var data = { ok: 1 };
|
||||
|
||||
var result = env.validate(schema, data);
|
||||
|
||||
if (result) {
|
||||
var errors = je(schema, data, result);
|
||||
console.log(JSON.stringify(errors, null, 4));
|
||||
}
|
||||
|
||||
errors.forEach(error =>
|
||||
console.log(
|
||||
'code: %s, message: %s, data: %s, path: %s',
|
||||
error.code,
|
||||
error.message,
|
||||
error.data,
|
||||
error.path));
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
// Type definitions for JJVE v0.4.0
|
||||
// Project: https://github.com/silas/jjve
|
||||
// Definitions by: Wim Looman <https://github.com/Nemo157>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../jjv/jjv.d.ts" />
|
||||
|
||||
declare module 'jjve' {
|
||||
import jjv = require('jjv');
|
||||
|
||||
function jjve(jjv: jjv.Env): jjve.Env;
|
||||
module jjve {
|
||||
interface Issue {
|
||||
code: string;
|
||||
message: string;
|
||||
data: any;
|
||||
path: string;
|
||||
}
|
||||
|
||||
interface Env {
|
||||
(schema: Object, data: any, errors: jjv.Errors): Issue[];
|
||||
}
|
||||
}
|
||||
|
||||
export = jjve;
|
||||
}
|
||||
Reference in New Issue
Block a user