Skip to content

Commit

Permalink
Updated Skmatc TypeScript definition
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed May 26, 2015
1 parent 7021652 commit ff4e8c1
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 55 deletions.
2 changes: 0 additions & 2 deletions lib/Model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/// <reference path="../_references.d.ts" />
import MongoDB = require('mongodb');
import Skmatc = require('skmatc');
import Concoction = require('concoction');
import Bluebird = require('bluebird');
import util = require('util');
import _ = require('lodash');
Expand Down
2 changes: 1 addition & 1 deletion lib/ModelHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ModelHandlers<TDocument extends { _id?: any }, TInstance> {
return Bluebird.all(documents.map((document: any) => {
return Bluebird.resolve().then(() => {
if (this.model.hooks.onRetrieved) this.model.hooks.onCreating(document);
var validation: SkmatcCore.IResult = this.model.helpers.validate(document);
var validation: Skmatc.Result = this.model.helpers.validate(document);
if (validation.failed) return Bluebird.reject(validation.error);
return document;
});
Expand Down
9 changes: 4 additions & 5 deletions lib/ModelHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/// <reference path="../_references.d.ts" />
import MongoDB = require('mongodb');
import Model = require('./Model');
import Concoction = require('concoction');
import Skmatc = require('skmatc');
import skmatc = require('skmatc');
import Omnom = require('./utils/Omnom');
import _ = require('lodash');
import Bluebird = require('bluebird');
Expand All @@ -11,17 +10,17 @@ export = ModelHelpers;

class ModelHelpers<TDocument extends { _id?: any }, TInstance> {
constructor(public model: Model<TDocument, TInstance>) {
this._validator = new Skmatc(model.schema);
this._validator = new skmatc(model.schema);
}

private _validator: Skmatc;
private _validator: Skmatc.Skmatc;

/**
* Validates a document to ensure that it matches the model's ISchema requirements
* @param {any} document The document to validate against the ISchema
* @returns {SkmatcCore.IResult} The result of the validation
*/
validate(document: TDocument): SkmatcCore.IResult {
validate(document: TDocument): Skmatc.Result {
return this._validator.validate(document);
}

Expand Down
7 changes: 5 additions & 2 deletions lib/ModelOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ export interface RemoveOptions {
}

export interface IModelOptions<TDocument, TInstance> {
validators?: SkmatcCore.IValidator[];
validators?: Skmatc.Validator[];
cache?: CacheDirector;
indexes?: (Index.Index | Index.IndexSpecification)[];
properties?: { [key: string]: (General.PropertyGetter<any> | General.Property<any>) };
identifier?: Concoction.Ingredient;
identifier?: {
apply(fromSource: any): any;
reverse(toSource: any): any;
};
}
2 changes: 1 addition & 1 deletion lib/Plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export = IPlugin;
interface IPlugin {
newModel? (model: ModelInterfaces.IModel<any, any>);
newInstance? (instance: any, model: ModelInterfaces.IModelBase);
validate?: SkmatcCore.IValidator | SkmatcCore.IValidator[];
validate?: Skmatc.Validator | Skmatc.Validator[];
}
81 changes: 37 additions & 44 deletions typings/skmatc/skmatc.d.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,65 @@
declare module "skmatc" {
export = Skmatc;
export = Skmatc.Skmatc;
}

declare class Skmatc {
constructor(schema: any);

static validators: [SkmatcCore.IValidator];
static Validator: SkmatcCore.IValidatorStatic;
static Result: SkmatcCore.IResultStatic;
static Failure: SkmatcCore.IFailureStatic;
static create(handles: (schema: any) => boolean, validate: (schema: any, data: any, path: string) => SkmatcCore.IResult, options?: any): SkmatcCore.IValidator;
static validate(validators: [SkmatcCore.IValidator], schema: any, data: any, path?: string): SkmatcCore.IResult;
static register(validator: SkmatcCore.IValidator);

schema: any;
validators: [SkmatcCore.IValidator];
validate(data: any, path?: string): SkmatcCore.IResult;
register(validator: SkmatcCore.IValidator);
}

declare module SkmatcCore {
interface IValidatorStatic {
(skmatc: Skmatc, options?: any): IValidator;
create(handles: (schema: any) => boolean, validate: (schema: any, data: any, path: string) => IResult, options?: any): IValidator;
module(handles: (schema: any) => boolean, validate: (schema: any, data: any, path: string) => IResult, options?: any): IValidator;
declare module Skmatc {
export class Skmatc {
constructor(schema: any);

static validators: [Validator];
static Validator: typeof Validator;
static Result: typeof Result;
static Failure: typeof Failure;
static create(handles: (schema: any) => boolean, validate: (schema: any, data: any, path: string) => Result, options?: any): Validator;
static validate(validators: [Validator], schema: any, data: any, path?: string): Result;
static register(validator: Validator);

schema: any;
validators: [Validator];
validate(data: any, path?: string): Result;
register(validator: Validator);
}

export class Validator {
constructor(skmatc: Skmatc, options?: any);
static create(handles: (schema: any) => boolean, validate: (schema: any, data: any, path: string) => Result, options?: any): Validator;
static module(handles: (schema: any) => boolean, validate: (schema: any, data: any, path: string) => Result, options?: any): Validator;

interface IValidator {
name: string;
skmatc: Skmatc;
handles(schema: any): boolean;
validate(schema: any, data: any, path: string): IResult;
assert(schema: any, data: any, path: string, test: boolean, message?: string): IResult;
fail(schema: any, data: any, path: string, message?: string): IResult;
validate(schema: any, data: any, path: string): Result;
assert(schema: any, data: any, path: string, test: boolean, message?: string): Result;
fail(schema: any, data: any, path: string, message?: string): Result;
}

interface IResultStatic {
(failures: [IFailure]): IResult;
compound(results: [IResult]): IResult;
}

interface IResult {
failures: [IFailure];

export class Result {
constructor(failures: [Failure]);
static compound(results: [Result]): Result;

failures: [Failure];
success: boolean;
failed: boolean;
message: string;
messages: [string];
error: Error;
}

interface IFailureStatic {
(validator: IValidator, schema: any, data: any, path: string, message?: string): IFailure;
}

interface IFailure {
validator: IValidator;

export class Failure {
constructor(validator: Validator, schema: any, data: any, path: string, message?: string);
validator: Validator;
schema: any;
data: any;
path: string;
message: string;
}

interface IValidationHandler {
export interface IValidationHandler {
(thisArg: {
validator: IValidationHandler;
skmatc: Skmatc;
fail(message?: string);
assert(test: boolean, message?: string);
}, schema: any, data: any, path: string): IResult;
}, schema: any, data: any, path: string): Result;
}
}

0 comments on commit ff4e8c1

Please sign in to comment.