From 0737d4dccb2f285c50b77da91dcf526e6fcf5347 Mon Sep 17 00:00:00 2001 From: HowardRichards Date: Wed, 16 Apr 2014 14:30:07 +0100 Subject: [PATCH] Added more static valerie namespace definitions --- valerie/valerie-tests.ts | 37 +++++++++++ valerie/valerie.d.ts | 128 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 160 insertions(+), 5 deletions(-) diff --git a/valerie/valerie-tests.ts b/valerie/valerie-tests.ts index 22adc7a13..b33932e29 100644 --- a/valerie/valerie-tests.ts +++ b/valerie/valerie-tests.ts @@ -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 = {}; + var t4 = valerie.validationState.setFor({}, state); +} diff --git a/valerie/valerie.d.ts b/valerie/valerie.d.ts index 1140d0a94..1514f3a4d 100644 --- a/valerie/valerie.d.ts +++ b/valerie/valerie.d.ts @@ -3,13 +3,13 @@ // Definitions by: Howard Richards // Definitions: https://github.com/borisyankov/DefinitelyTyped -/// + /** * * 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 { @@ -246,9 +246,78 @@ declare module Valerie { // (value should be observable or computed) validatableProperty(value: T, options?: ValidationOptions): PropertyValidationState; + // 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(value: any): any[]; + + // Creates a function that returns the given value, or simply returns the given value if it is already a function + asFunction(value: T): () => T; + asFunction(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.
* [fluent] @@ -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 {