diff --git a/js-schema/js-schema-tests.ts b/js-schema/js-schema-tests.ts new file mode 100644 index 000000000..4a9a33e58 --- /dev/null +++ b/js-schema/js-schema-tests.ts @@ -0,0 +1,19 @@ +/// + +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 diff --git a/js-schema/js-schema.d.ts b/js-schema/js-schema.d.ts new file mode 100644 index 000000000..10c1460f0 --- /dev/null +++ b/js-schema/js-schema.d.ts @@ -0,0 +1,50 @@ +// Type definitions for js-schema +// Project: https://github.com/molnarg/js-schema +// Definitions by: Marcin Porebski +// 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): 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; +}