From 363e73e7efb745e4b8896db8a7000f91bc141aca Mon Sep 17 00:00:00 2001 From: Adam Meadows Date: Mon, 16 Nov 2015 04:24:42 -0800 Subject: [PATCH] Added z-schema project --- z-schema/z-schema-tests.ts | 34 +++++++++++++++++++++ z-schema/z-schema.d.ts | 62 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 z-schema/z-schema-tests.ts create mode 100644 z-schema/z-schema.d.ts diff --git a/z-schema/z-schema-tests.ts b/z-schema/z-schema-tests.ts new file mode 100644 index 0000000000..90fdc4cd32 --- /dev/null +++ b/z-schema/z-schema-tests.ts @@ -0,0 +1,34 @@ +/// + +import ZSchema = require('z-schema'); + +var options: ZSchema.Options = { + noTypeless: true, + forceItems: true, +}; + +var validator: ZSchema.Validator = new ZSchema(options); +var json: any = { + foo: 'bar', +}; + +var schema: any = { + 'type': 'object', + properties: { + foo: { + 'type': 'string', + }, + }, +}; + +validator.validate(json, schema); +validator.validate(json, schema, function (err: any, valid: boolean) { + if (err) { + console.log(err); + } else { + console.log('valid = ' + valid); + } +}); + +var error: ZSchema.SchemaError = validator.getLastError(); +var errors: ZSchema.SchemaError[] = validator.getLastErrors(); diff --git a/z-schema/z-schema.d.ts b/z-schema/z-schema.d.ts new file mode 100644 index 0000000000..4e8d673c5d --- /dev/null +++ b/z-schema/z-schema.d.ts @@ -0,0 +1,62 @@ +// Type definitions for z-schema v3.16.0 +// Project: https://github.com/zaggino/z-schema +// Definitions by: Adam Meadows +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare module ZSchema { + + export interface Options { + asyncTimeout?: number; + forceAdditional?: boolean; + assumeAdditional?: boolean; + forceItems?: boolean; + forceMinItems?: boolean; + forceMaxItems?: boolean; + forceMinLength?: boolean; + forceMaxLength?: boolean; + forceProperties?: boolean; + ignoreUnresolvableReferences?: boolean; + noExtraKeywords?: boolean; + noTypeless?: boolean; + noEmptyStrings?: boolean; + noEmptyArrays?: boolean; + strictUris?: boolean; + strictMode?: boolean; + reportPathAsArray?: boolean; + breakOnFirstError?: boolean; + pedanticCheck?: boolean; + ignoreUnknownFormats?: boolean; + } + + export interface SchemaError { + code: string; + description: string; + message: string; + params: string[]; + path: string; + } + + export class Validator { + constructor(options: Options); + + /** + * @param json - either a JSON string or a parsed JSON object + * @param schema - the JSON object representing the schema + * @returns true if json matches schema + */ + validate(json: any, schema: any): boolean; + + /** + * @param json - either a JSON string or a parsed JSON object + * @param schema - the JSON object representing the schema + */ + validate(json: any, schema: any, callback: (err: any, valid: boolean) => void): void; + + getLastError(): SchemaError; + getLastErrors(): SchemaError[]; + } +} + +declare module "z-schema" { + export = ZSchema.Validator; +}