Merge pull request #7918 from marcinporebski/master

js-schema definitions
This commit is contained in:
Horiuchi_H
2016-02-05 10:50:42 +09:00
2 changed files with 69 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
///<reference path="js-schema.d.ts" />
import {default as schema} from 'js-schema';
var Duck = schema({ // A duck
swim : Function, // - can swim
quack : Function, // - can quack
age : Number.min(0).max(5), // - is 0 to 5 years old
color : ['yellow', 'brown'] // - has either yellow or brown color
});
// Some animals
var myDuck = { swim : function() {}, quack : function() {}, age : 2, color : 'yellow' },
myCat = { walk : function() {}, purr : function() {}, age : 3, color : 'black' },
animals = [ myDuck, myCat, {}, /*...*/ ];
// Simple checks
console.log( Duck(myDuck) ); // true
console.log( Duck(myCat) ); // false
+50
View File
@@ -0,0 +1,50 @@
// Type definitions for js-schema
// Project: https://github.com/molnarg/js-schema
// Definitions by: Marcin Porebski <https://github.com/marcinporebski>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module 'js-schema'
{
export interface Schema
{
(obj: any): boolean; // test obj against the schema
}
export default function schema(definition: any): Schema
}
interface NumberConstructor
{
min(n: number): NumberConstructor;
max(n: number): NumberConstructor;
below(n: number): NumberConstructor;
above(n: number): NumberConstructor;
step(n: number): NumberConstructor;
}
interface StringConstructor
{
of(charset: string): StringConstructor;
of(length: number, charset: string): StringConstructor;
of(minLength: number, maxLength: number, charset: string): StringConstructor;
}
interface ArrayConstructor
{
like(arr: Array<any>): ArrayConstructor;
of(pattern: any): ArrayConstructor;
of(length: number, pattern: any): ArrayConstructor;
of(minLength: number, maxLength: number, pattern: any): ArrayConstructor;
}
interface ObjectConstructor
{
like(obj: any): ObjectConstructor;
reference(obj: any): ObjectConstructor;
}
interface FunctionConstructor
{
reference(func: Function): FunctionConstructor;
}