From d475c0d61172fa78c0f458dc5944ecac15e00b29 Mon Sep 17 00:00:00 2001 From: Marcin Porebski Date: Mon, 1 Feb 2016 13:04:41 +0100 Subject: [PATCH 1/2] js-schema patterns definitions --- js-schema/js-schema-tests.ts | 19 ++++++++++++++ js-schema/js-schema.d.ts | 50 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 js-schema/js-schema-tests.ts create mode 100644 js-schema/js-schema.d.ts 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..5b9e35d6b --- /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 Porębski 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): 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; +} \ No newline at end of file From 7e9ed1aedc86bea452bd72bb50f8420146fc6e8e Mon Sep 17 00:00:00 2001 From: Marcin Porebski Date: Mon, 1 Feb 2016 13:08:05 +0100 Subject: [PATCH 2/2] js-schema patterns definitions (doc-header fix?) --- js-schema/js-schema.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js-schema/js-schema.d.ts b/js-schema/js-schema.d.ts index 5b9e35d6b..10c1460f0 100644 --- a/js-schema/js-schema.d.ts +++ b/js-schema/js-schema.d.ts @@ -1,6 +1,6 @@ // Type definitions for js-schema // Project: https://github.com/molnarg/js-schema -// Definitions by: Marcin Porębski https://github.com/marcinporebski +// Definitions by: Marcin Porebski // Definitions: https://github.com/borisyankov/DefinitelyTyped @@ -47,4 +47,4 @@ interface ObjectConstructor interface FunctionConstructor { reference(func: Function): FunctionConstructor; -} \ No newline at end of file +}