From 1af3e2d2ad3146fae2e19e6fde70bb36098f6c7f Mon Sep 17 00:00:00 2001 From: Kai Ilbertz Date: Sun, 1 May 2016 15:15:25 +0200 Subject: [PATCH] Add type definition for valdr (https://www.npmjs.com/package/valdr) --- valdr/valdr-message-tests.ts | 26 +++++++++++++ valdr/valdr-message.d.ts | 74 +++++++++++++++++++++++++++++++++++ valdr/valdr-tests.ts | 38 ++++++++++++++++++ valdr/valdr.d.ts | 75 ++++++++++++++++++++++++++++++++++++ 4 files changed, 213 insertions(+) create mode 100644 valdr/valdr-message-tests.ts create mode 100644 valdr/valdr-message.d.ts create mode 100644 valdr/valdr-tests.ts create mode 100644 valdr/valdr.d.ts diff --git a/valdr/valdr-message-tests.ts b/valdr/valdr-message-tests.ts new file mode 100644 index 0000000000..7bb2da6b0f --- /dev/null +++ b/valdr/valdr-message-tests.ts @@ -0,0 +1,26 @@ +/// + +function ValdrMessageTests() { + var valdrMessage: valdr.message.IValdrMessage; + + valdrMessage.templateUrl = 'valdrMesssageTemplate.html'; + valdrMessage.translateAvailable = true; + valdrMessage.angularMessagesEnabled = true; + valdrMessage.setTemplate('
{{ violation.message }}
'); + valdrMessage.addMessages({ + 'required': 'This field is required.', + 'number': 'Not a valid number.' + }); + var message = valdrMessage.getMessage("person", "lastName", "required"); +} + +function ValdrMessageProviderTests() { + var valdrMessageProvider: valdr.message.IValdrMessageProvider; + valdrMessageProvider.setTemplate('
{{ violation.message }}
'); + valdrMessageProvider.setTemplateUrl('valdrMesssageTemplate.html'); + valdrMessageProvider.addMessages({ + 'person.lastName.required': 'This last name is required.', + 'person.age.number': 'The age has to be a number.' + }); + var message = valdrMessageProvider.getMessage("person", "lastName", "required"); +} \ No newline at end of file diff --git a/valdr/valdr-message.d.ts b/valdr/valdr-message.d.ts new file mode 100644 index 0000000000..2abaf99be2 --- /dev/null +++ b/valdr/valdr-message.d.ts @@ -0,0 +1,74 @@ +// Type definitions for valdr.js v1.1.5 (valdr-message) +// Project: https://github.com/netceteragroup/valdr +// Definitions by: Kai Ilbertz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace valdr.message { + + interface IValdrMessage { + /** + * Default message template URL. + */ + templateUrl: string; + + /** + * A boolean that shows if Angular-translate is available. + */ + translateAvailable: boolean; + + /** + * A boolean that allows to show messages for the AngularJS built-in validators like 'required' or 'number'. + */ + angularMessagesEnabled: boolean; + + /** + * Sets the default message template. + * @param template the default message template (eg "
{{ violation.message }}
"). + */ + setTemplate(template: string): void; + + /** + * Adds messages for AngularJS build-in validators (eg "required" and "number") or for specific fields (eg "Person.lastName.required"). + * @param messages the list of key/value pairs. + */ + addMessages(messages: any): void; + + /** + * Gets the validator message. + * @param typeName the name of the type. + * @param fieldName the name of the field. + * @param validatorName the name of the validator. + * @returns {string} the message. + */ + getMessage(typeName: string, fieldName: string, validatorName: string): string; + } + + interface IValdrMessageProvider { + /** + * Sets the default message template. + * @param template the default message template (eg "
{{ violation.message }}
"). + */ + setTemplate(template: string): void; + + /** + * Sets the URL for the default message template (eg "/partials/valdrMesssageTemplate.html"). + * @param url the URL of the default message template. + */ + setTemplateUrl(url: string): void; + + /** + * Adds messages for AngularJS build-in validators (eg "required" and "number") or for specific fields (eg "Person.lastName.required"). + * @param messages the list of key/value pairs. + */ + addMessages(messages: any): void; + + /** + * Gets the validator message. + * @param typeName the name of the type. + * @param fieldName the name of the field. + * @param validatorName the name of the validator. + * @returns {string} the message. + */ + getMessage(typeName: string, fieldName: string, validatorName: string): string; + } +} \ No newline at end of file diff --git a/valdr/valdr-tests.ts b/valdr/valdr-tests.ts new file mode 100644 index 0000000000..e0e1860718 --- /dev/null +++ b/valdr/valdr-tests.ts @@ -0,0 +1,38 @@ +/// + +function ValdrTests() { + var valdr: valdr.IValdr; + var validation = valdr.validate('person', 'lastName', 'test'); + valdr.addConstraints({ + 'person': { + 'lastName': { + 'required': { + 'message': 'Last name is required.' + } + } + } + }); + valdr.removeConstraints('person'); + var constraints = valdr.getConstraints(); + valdr.setClasses({ + valid: 'demo-is-valid', + invalid: 'demo-is-invalid' + }); +} + +function ValdrProviderTests() { + var valdrProvider: valdr.IValdrProvider; + valdrProvider.addConstraints({ + 'person': { + 'lastName': { + 'required': { + 'message': 'Last name is required.' + } + } + } + }); + valdrProvider.removeConstraints('person'); + valdrProvider.setConstraintUrl('/api/constraints'); + valdrProvider.addValidator('customValidator'); + valdrProvider.addConstraintAlias('customValidator', 'customValidatorAlias'); +} \ No newline at end of file diff --git a/valdr/valdr.d.ts b/valdr/valdr.d.ts new file mode 100644 index 0000000000..29ac2116d2 --- /dev/null +++ b/valdr/valdr.d.ts @@ -0,0 +1,75 @@ +// Type definitions for valdr.js v1.1.5 +// Project: https://github.com/netceteragroup/valdr +// Definitions by: Kai Ilbertz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace valdr { + + interface IValdr { + /** + * Validates the value of the given type with the constraints for the given field name. + * @param typeName the type name. + * @param fieldName the field name. + * @param value the value to validate. + * @returns {*} the validation result. + */ + validate(typeName: string, fieldName: string, value: string): any; + + /** + * Adds a new list of constraints (JSON Object). + * @param newConstraints the list of constraints (JSON Object). + */ + addConstraints(newConstraints: any): void; + + /** + * Removes one or many contraints. + * @param constraintNames the name or array of constraint names. + */ + removeConstraints(constraintNames: string | string[]): void; + + /** + * Gets the list of constraints (JSON Object). + * @returns the list of constraints. + */ + getConstraints(): any; + + /** + * Sets custom classes on the surrounding elements. + * @param newClasses the new classes. + */ + setClasses(newClasses: { valid: string, invalid: string }): void; + } + + interface IValdrProvider { + /** + * Adds a new list of constraints (JSON Object). + * @param newConstraints the list of constraints (JSON Object). + */ + addConstraints(newConstraints: any): void; + + /** + * Removes one or many contraints. + * @param constraintNames the name or array of constraint names. + */ + removeConstraints(constraintNames: string | string[]): void; + + /** + * Sets the constraint URL (eg "/api/constraints"). + * @param url the URL of the contraints. + */ + setConstraintUrl(url: string): void; + + /** + * Adds a custom validator. + * @param validatorName the name of the custom validator. + */ + addValidator(validatorName: string): void; + + /** + * Adds a constraint validator alias. + * @param valdrName the validator name. + * @param alias the validator alias name. + */ + addConstraintAlias(valdrName: string, alias: string): void; + } +} \ No newline at end of file