Added more static valerie namespace definitions

This commit is contained in:
HowardRichards
2014-04-16 14:30:07 +01:00
parent 64a51e86db
commit 0737d4dccb
2 changed files with 160 additions and 5 deletions
+37
View File
@@ -296,3 +296,40 @@ function ModelValidation() {
.end();
}
function UtilsStaticTests() {
var t1 = valerie.utils.asArray(1);
var t2 = valerie.utils.asArray([1,2]);
var t3 = valerie.utils.asFunction(1);
var t4 = valerie.utils.asFunction(() => { return 1; });
var t5 = valerie.utils.isArray([1, 2]);
var t5 = valerie.utils.isArrayOrObject(1);
var t6 = valerie.utils.isFunction("x");
var t7 = valerie.utils.isMissing(null);
var t8 = valerie.utils.isObject({});
var t9 = valerie.utils.isString("test");
var opts: Valerie.ValidationOptions = {}; // all values are optional
var t10 = valerie.utils.mergeOptions(opts, opts);
}
function ValidationResultStaticTests() {
var t1 = valerie.ValidationResult.passedInstance;
var t2 = valerie.ValidationResult.createFailedResult("message");
}
function ValidationStateStaticTests() {
var t1 = valerie.validationState.findIn({});
var t2 = valerie.validationState.getFor({});
var t3 = valerie.validationState.has({});
var state = <Valerie.IValidationState>{};
var t4 = valerie.validationState.setFor({}, state);
}
+123 -5
View File
@@ -3,13 +3,13 @@
// Definitions by: Howard Richards <https://github.com/conficient>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../knockout/knockout.d.ts" />
/**
*
* Extensions to KO functions to provide validation
*
* Version 1.1 - added missing methods to ModelValidationState
* Version 1.2 - added more static methods to valerie object
*
*/
interface KnockoutObservable<T> {
@@ -246,9 +246,78 @@ declare module Valerie {
// (value should be observable or computed)
validatableProperty<T>(value: T, options?: ValidationOptions): PropertyValidationState<T>;
// Validation result class
ValidationResult: ValidationResultStatic;
// additional namespaces for static methods:
converters: ConvertersStatic;
/*
//TODO: additional namespaces/statics not yet used
dom: DomStatic;
formatting: FormattingStatic;
koBindingsHelper: KoBindingsHelperStatic;
koExtras: KoExtrasStatic;
rules: RulesStatic;
*/
utils: UtilsStatic;
validationState: ValidationState;
}
interface ValidationResultStatic {
passedInstance: ValidationResult;
// static method to create validatio failed message
createFailedResult(message: string): ValidationResult;
}
// Contains converters, always singletons.
interface ConvertersStatic {
//TODO: other converters to be added
passThrough: Valerie.IConverter;
}
interface UtilsStatic {
// Creates a function that returns the given value as an array of one item, or simply returns the given value if it is already an array.
asArray<T>(value: any): any[];
// Creates a function that returns the given value, or simply returns the given value if it is already a function
asFunction<T>(value: T): () => T;
asFunction<T>(fn: () => T): () => T;
// Tests whether the given value is an array
isArray(value: any): boolean;
// Tests whether the given value is an array or object.
isArrayOrObject(value: any): boolean;
// Tests whether the given value is a function.
isFunction(value: any): boolean;
// Tests whether the given value is "missing".undefined, null, an empty string or an empty array are considered to be "missing".
isMissing(value: any): boolean;
// Tests whether the given value is an object.
isObject(value: any): boolean;
// Tests whether the give value is a string.
isString(value: any): boolean;
//Merges the given default options with the given options.
// - either parameter can be omitted and a clone of the other parameter will be returned
// - the merge is shallow
// - array properties are shallow cloned
mergeOptions(defaultOptions: ValidationOptions, options): ValidationOptions;
}
// callback interface (see mapModel above)
@@ -296,6 +365,53 @@ declare module Valerie {
*/
clearSummary(valueOrFunction: any): ModelValidationState;
/***
* Gets whether the model has failed validation.
* @return {boolean}
*/
failed(): boolean;
/***
* Gets the validation states that belong to the model that are in a failure state.
* @return {Valerie.IValidationState[]}
*/
failedStates(): Valerie.IValidationState[];
/***
* Gets the name of the model.
* @return {string}
*/
getName(): string;
isApplicable(): boolean;
message(): string;
passed(): boolean;
/***
* Gets or sets whether the computation that updates the validation result has been paused.
* @param {boolean} [value = false] true if the computation should be paused, false if the computation should not be paused
* @return {boolean} true if computation is paused, false otherwise
*/
paused(value: boolean): boolean;
pending(): boolean;
pendingStates(): IValidationState[];
refresh(): void;
result(): ValidationResult;
summary(): summaryItem[]
/***
* Gets or sets whether the model has been 'touched' by user action
*/
touched(value: boolean): boolean;
validationStates(): IValidationState[];
/**
* Includes any validation failures for this model in a validation summary.<br/>
* <i>[fluent]</i>
@@ -501,9 +617,6 @@ declare module Valerie {
pending: boolean; //true if the activity hasn't yet completed
message: string; //a message from the activity
new: (state: any, message?: string) => ValidationResult;
//TODO: not added static members/methods
createFailedResult(message: string): ValidationResult;
}
interface IRule {
@@ -598,6 +711,11 @@ declare module Valerie {
// Sets the validation state for the given model, observable or computed.
setFor(modelOrObservableOrComputed: any, state: IValidationState): void;
}
interface summaryItem {
name: string;
message: string;
}
}
declare module Valerie.Rules {