Skip to content

Commit

Permalink
updated the form validator creaton
Browse files Browse the repository at this point in the history
  • Loading branch information
egandro committed Sep 5, 2017
1 parent 75586e8 commit d324514
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
47 changes: 39 additions & 8 deletions lib/generator/angular.client/templates/formvalidators.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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(<any>(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;
Expand Down
17 changes: 10 additions & 7 deletions lib/swagger/entites/propertytype.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"repository": {
"type": "git",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit d324514

Please sign in to comment.