From d32451433756b7898aa8c937e301087dfeee9eb6 Mon Sep 17 00:00:00 2001 From: Harald Fielker Date: Wed, 6 Sep 2017 00:05:35 +0200 Subject: [PATCH] updated the form validator creaton --- .../templates/formvalidators.ts.ejs | 47 +++++++++++++++---- lib/swagger/entites/propertytype.class.ts | 17 ++++--- package.json | 4 +- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/lib/generator/angular.client/templates/formvalidators.ts.ejs b/lib/generator/angular.client/templates/formvalidators.ts.ejs index 368c50e..d5b3d69 100644 --- a/lib/generator/angular.client/templates/formvalidators.ts.ejs +++ b/lib/generator/angular.client/templates/formvalidators.ts.ejs @@ -27,24 +27,55 @@ type.properties.forEach(function(prop){%><% }; public static getForm(entity: string): any { - if (ChemEasyAngularServiceValidators.definitions.hasOwnProperty(entity)) { - return ChemEasyAngularServiceValidators.definitions[entity]; + const result = {}; + if (<%- ngModuleName%>Validators.definitions.hasOwnProperty(entity)) { + const data = <%- ngModuleName%>Validators.definitions[entity]; + Object.keys(data).forEach((key) => { + const item = data[key]; + let validator: any = null; + if (item.length > 1) { + const validators = item[1]; + if (validators.length === 1) { + validator = validators[0]; + } else { + validator = Validators.compose(validators); + } + } + result[key] = []; + result[key].push(item[0]); + if (validator) { + result[key].push(validator); + } + }); } - return {}; + return result; } - public static getColumnValidator(entity: string, column: string): any { + public static getColumnValidator(entity: string, column: string, ...customValidators: any[]): any { let result: any = Validators.compose((null)); - if (!ChemEasyAngularServiceValidators.definitions.hasOwnProperty(entity)) { + if (!<%- ngModuleName%>Validators.definitions.hasOwnProperty(entity)) { return result; } - const data = ChemEasyAngularServiceValidators.definitions[entity]; + const data = <%- ngModuleName%>Validators.definitions[entity]; if (!data.hasOwnProperty(column)) { return result; - } else { - result = data[column][1]; + } + + if (data[column].length < 2) { + return result; + } + + result = data[column][1]; + if (customValidators) { + for (const validator of customValidators) { + result.push(validator); + } + } + + if (result.length > 1) { + result.push(Validators.compose(result)); } return result; diff --git a/lib/swagger/entites/propertytype.class.ts b/lib/swagger/entites/propertytype.class.ts index 54d1459..ee53e28 100644 --- a/lib/swagger/entites/propertytype.class.ts +++ b/lib/swagger/entites/propertytype.class.ts @@ -50,17 +50,20 @@ export class PropertyType { if (this.pattern != null && this.pattern !== undefined) { validators.push('Validators.pattern(/' + this.pattern + '/)'); } - if (validators.length == 0) { + const length = validators.length; + if (length == 0) { return ''; } - if (validators.length == 1) { - return validators[0]; - } - let result = 'Validators.compose(['; + let result = '['; + let count = 0; for (let validator of validators) { - result += validator + ', '; + result += validator; + count++; + if (count < length) { + result += ', '; + } } - result += '])' + result += ']' return result; } diff --git a/package.json b/package.json index 045cddb..84c02c5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "nicassa-generator", "description": "CLI based code generator for Sequelize, Node, Typescript, Angular 2, Android, Swagger, ORM Lite", - "version": "0.0.38", + "version": "0.0.39", "author": "Harald Fielker ", "repository": { "type": "git", @@ -32,7 +32,7 @@ "ejs": "^2.5.6", "fs-force-mkdir-sync": "^1.1.0", "identifierfy": "^1.1.1", - "nicassa-parser-db": "^0.0.8", + "nicassa-parser-db": "^0.0.9", "nomnom": "^1.8.1", "prompt": "^1.0.0", "swagger-parser": "^3.4.1",