Skip to content

Commit

Permalink
add ValidatorOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Oct 23, 2023
1 parent 69e8c39 commit ae3c3a7
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 104 deletions.
2 changes: 1 addition & 1 deletion generators/jdl/generator.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe(`generator - ${generator}`, () => {
inline: 'entity Foo {}',
baseName: 'jhipster',
}),
).rejects.toThrow('Database type is required to validate entities.');
).rejects.toThrow("The JDL object, the application's name and its the database type are mandatory.");
});
it('without baseName should reject', async () => {
await expect(
Expand Down
11 changes: 5 additions & 6 deletions jdl/validators/entity-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/

import Validator from './validator.js';
import Validator, { ValidatorOptions } from './validator.js';
import { reservedKeywords } from '../jhipster/index.mjs';

const { isReservedClassName } = reservedKeywords;
Expand All @@ -27,12 +27,11 @@ export default class EntityValidator extends Validator {
super('entity', ['name', 'tableName']);
}

validate(jdlEntity) {
validate(jdlEntity, options: ValidatorOptions = {}) {
super.validate(jdlEntity);
}

validateBusiness(jdlEntity) {
checkForReservedClassName(jdlEntity);
if (options.checkReservedKeywords) {
checkForReservedClassName(jdlEntity);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions jdl/validators/enum-validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ describe('jdl - EnumValidator', () => {
expect(() => validator.validate({})).to.throw(/^The enum attribute name was not found\.$/);
});
});
context('with a reserved class name as name', () => {
context('passing checkReservedKeywords with a reserved class name as name', () => {
it('should fail', () => {
expect(() => {
validator.validateBusiness(new JDLEnum({ name: 'Catch' }));
validator.validate(new JDLEnum({ name: 'Catch' }), { checkReservedKeywords: true });
}).to.throw(/^The enum name 'Catch' is reserved keyword and can not be used as enum class name\.$/);
});
});
Expand Down
11 changes: 5 additions & 6 deletions jdl/validators/enum-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*/

import Validator from './validator.js';
import Validator, { ValidatorOptions } from './validator.js';
import { reservedKeywords } from '../jhipster/index.mjs';

const { isReservedClassName } = reservedKeywords;
Expand All @@ -26,12 +26,11 @@ export default class EnumValidator extends Validator {
super('enum', ['name']);
}

validate(jdlEnum) {
validate(jdlEnum, options: ValidatorOptions = {}) {
super.validate(jdlEnum);
}

validateBusiness(jdlEnum) {
checkForReservedClassName(jdlEnum);
if (options.checkReservedKeywords) {
checkForReservedClassName(jdlEnum);
}
}
}

Expand Down
44 changes: 10 additions & 34 deletions jdl/validators/jdl-with-application-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import JDLObject from '../models/jdl-object.js';
import JDLRelationship from '../models/jdl-relationship.js';
import JDLApplication from '../models/jdl-application.js';
import ListJDLApplicationConfigurationOption from '../models/list-jdl-application-configuration-option.js';
import { ValidatorOptions } from './validator.js';

const { OptionNames } = applicationOptions;

Expand All @@ -49,17 +50,16 @@ export default function createValidator(jdlObject: JDLObject, logger: any = cons
return {
checkForErrors: () => {
jdlObject.forEachApplication(jdlApplication => {
const blueprints = jdlApplication.getConfigurationOptionValue(BLUEPRINTS);
const checkReservedKeywords = (blueprints?.length ?? 0) === 0;
checkForBlueprintConfigErrors(jdlApplication);
checkForRelationshipErrors();
checkForEntityErrors(jdlApplication);
checkForEnumErrors();
const blueprints = jdlApplication.getConfigurationOptionValue(BLUEPRINTS);
if (blueprints && blueprints.length > 0) {
checkForEntityErrors(jdlApplication, { checkReservedKeywords });
checkForEnumErrors({ checkReservedKeywords });
if (!checkReservedKeywords) {
logger.warn('Blueprints are being used, the JDL validation phase is skipped.');
return;
}
checkForEntityBusinessErrors(jdlApplication);
checkForEnumBusinessErrors();
checkDeploymentsErrors();
checkForOptionErrors();
});
Expand All @@ -81,21 +81,7 @@ export default function createValidator(jdlObject: JDLObject, logger: any = cons
});
}

function checkForEntityErrors(jdlApplication) {
if (jdlObject.getEntityQuantity() === 0) {
return;
}
const validator = new EntityValidator();
jdlObject.forEachEntity(jdlEntity => {
if (!jdlApplication.hasEntityName(jdlEntity.name)) {
return;
}
validator.validate(jdlEntity);
checkForFieldErrors(jdlEntity.name, jdlEntity.fields, jdlApplication);
});
}

function checkForEntityBusinessErrors(jdlApplication) {
function checkForEntityErrors(jdlApplication, options: ValidatorOptions) {
if (jdlObject.getEntityQuantity() === 0) {
return;
}
Expand All @@ -104,7 +90,7 @@ export default function createValidator(jdlObject: JDLObject, logger: any = cons
if (!jdlApplication.hasEntityName(jdlEntity.name)) {
return;
}
validator.validateBusiness(jdlEntity);
validator.validate(jdlEntity, options);
checkForFieldErrors(jdlEntity.name, jdlEntity.fields, jdlApplication);
});
}
Expand Down Expand Up @@ -144,23 +130,13 @@ export default function createValidator(jdlObject: JDLObject, logger: any = cons
});
}

function checkForEnumErrors() {
if (jdlObject.getEnumQuantity() === 0) {
return;
}
const validator = new EnumValidator();
jdlObject.forEachEnum(jdlEnum => {
validator.validate(jdlEnum);
});
}

function checkForEnumBusinessErrors() {
function checkForEnumErrors(options: ValidatorOptions) {
if (jdlObject.getEnumQuantity() === 0) {
return;
}
const validator = new EnumValidator();
jdlObject.forEachEnum(jdlEnum => {
validator.validateBusiness(jdlEnum);
validator.validate(jdlEnum, options);
});
}

Expand Down
88 changes: 34 additions & 54 deletions jdl/validators/jdl-without-application-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import EntityValidator from './entity-validator.js';
import FieldValidator from './field-validator.js';
import { fieldTypes, applicationTypes, databaseTypes, binaryOptions, reservedKeywords, relationshipOptions } from '../jhipster/index.mjs';
import { fieldTypes, applicationTypes, databaseTypes, binaryOptions, relationshipOptions } from '../jhipster/index.mjs';
import ValidationValidator from './validation-validator.js';
import RelationshipValidator from './relationship-validator.js';
import EnumValidator from './enum-validator.js';
Expand All @@ -29,9 +29,10 @@ import BinaryOptionValidator from './binary-option-validator.js';

import JDLObject from '../models/jdl-object.js';
import JDLRelationship from '../models/jdl-relationship.js';
import { ValidatorOptions } from './validator.js';
import { isReservedFieldName, isReservedPaginationWords, isReservedTableName } from '../jhipster/reserved-keywords.js';

const { BUILT_IN_ENTITY } = relationshipOptions;
const { isReservedFieldName, isReservedTableName, isReservedPaginationWords } = reservedKeywords;
const { SQL } = databaseTypes;

/**
Expand All @@ -49,68 +50,57 @@ export default function createValidator(jdlObject: JDLObject, applicationSetting
if (!jdlObject) {
throw new Error('A JDL object must be passed to check for business errors.');
}

if (applicationSettings.blueprints && applicationSettings.blueprints.length !== 0) {
return {
checkForErrors: () => {
logger.warn('Blueprints are being used, the JDL validation phase is skipped.');
},
};
}
const { blueprints, databaseType } = applicationSettings;
const checkReservedKeywords = Boolean((databaseType ?? 'no') !== 'no') && (blueprints?.length ?? 0) === 0;

return {
checkForErrors: () => {
checkForEntityErrors();
checkForEntityErrors({ checkReservedKeywords });
checkForRelationshipErrors();
checkForEnumErrors();
checkForEntityBusinessErrors();
checkForEnumBusinessErrors();
checkDeploymentsErrors();
checkForOptionErrors();
checkForEnumErrors({ checkReservedKeywords });
if (checkReservedKeywords) {
checkDeploymentsErrors();
checkForOptionErrors();
} else {
logger.warn('Blueprints are being used, the JDL validation phase is skipped.');
}
},
};

function checkForEntityErrors() {
if (jdlObject.getEntityQuantity() === 0) {
return;
}
const validator = new EntityValidator();
jdlObject.forEachEntity(jdlEntity => {
validator.validate(jdlEntity);
checkForFieldErrors(jdlEntity.name, jdlEntity.fields);
});
}

function checkForEntityBusinessErrors() {
function checkForEntityErrors(options: ValidatorOptions) {
if (jdlObject.getEntityQuantity() === 0) {
return;
}
if (!applicationSettings.databaseType) {
throw new Error('Database type is required to validate entities.');
}
const validator = new EntityValidator();
jdlObject.forEachEntity(jdlEntity => {
validator.validateBusiness(jdlEntity);
if (isReservedTableName(jdlEntity.tableName, applicationSettings.databaseType)) {
logger.warn(`The table name '${jdlEntity.tableName}' is a reserved keyword, so it will be prefixed with the value of 'jhiPrefix'.`);
validator.validate(jdlEntity, options);
if (options.checkReservedKeywords) {
if (isReservedTableName(jdlEntity.tableName, applicationSettings.databaseType)) {
logger.warn(
`The table name '${jdlEntity.tableName}' is a reserved keyword, so it will be prefixed with the value of 'jhiPrefix'.`,
);
}
}
checkForFieldErrors(jdlEntity.name, jdlEntity.fields, options);
});
}

function checkForFieldErrors(entityName, jdlFields) {
function checkForFieldErrors(entityName, jdlFields, options: ValidatorOptions) {
const validator = new FieldValidator();
const filtering = applicationSettings.databaseType === SQL;

Object.keys(jdlFields).forEach(fieldName => {
const jdlField = jdlFields[fieldName];
validator.validate(jdlField);
if (isReservedFieldName(jdlField.name)) {
logger.warn(`The name '${jdlField.name}' is a reserved keyword, so it will be prefixed with the value of 'jhiPrefix'.`);
}
if (filtering && isReservedPaginationWords(jdlField.name)) {
throw new Error(
`Field name '${fieldName}' found in ${entityName} is a reserved keyword, as it is used by Spring for pagination in the URL.`,
);
if (options.checkReservedKeywords) {
if (isReservedFieldName(jdlField.name)) {
logger.warn(`The name '${jdlField.name}' is a reserved keyword, so it will be prefixed with the value of 'jhiPrefix'.`);
}
if (filtering && isReservedPaginationWords(jdlField.name)) {
throw new Error(
`Field name '${fieldName}' found in ${entityName} is a reserved keyword, as it is used by Spring for pagination in the URL.`,
);
}
}
const typeCheckingFunction = getTypeCheckingFunction(entityName, applicationSettings);
if (!jdlObject.hasEnum(jdlField.type) && !typeCheckingFunction(jdlField.type)) {
Expand Down Expand Up @@ -145,23 +135,13 @@ export default function createValidator(jdlObject: JDLObject, applicationSetting
});
}

function checkForEnumErrors() {
if (jdlObject.getEnumQuantity() === 0) {
return;
}
const validator = new EnumValidator();
jdlObject.forEachEnum(jdlEnum => {
validator.validate(jdlEnum);
});
}

function checkForEnumBusinessErrors() {
function checkForEnumErrors(options: ValidatorOptions) {
if (jdlObject.getEnumQuantity() === 0) {
return;
}
const validator = new EnumValidator();
jdlObject.forEachEnum(jdlEnum => {
validator.validateBusiness(jdlEnum);
validator.validate(jdlEnum, options);
});
}

Expand Down
4 changes: 3 additions & 1 deletion jdl/validators/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* limitations under the License.
*/

export type ValidatorOptions = { checkReservedKeywords?: boolean };

export default class Validator {
objectType: any;
fieldsToCheck: any;
Expand All @@ -26,7 +28,7 @@ export default class Validator {
this.fieldsToCheck = fieldsToCheck;
}

validate(object) {
validate(object, _options?: ValidatorOptions) {
if (!object) {
throw new Error(`No ${this.objectType}.`);
}
Expand Down

0 comments on commit ae3c3a7

Please sign in to comment.