mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-01 11:50:54 +08:00
Add typings for jjv
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user