diff --git a/generators/jdl/generator.spec.mts b/generators/jdl/generator.spec.mts index 04cb5432be99..cb8bbf129405 100644 --- a/generators/jdl/generator.spec.mts +++ b/generators/jdl/generator.spec.mts @@ -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( diff --git a/jdl/validators/entity-validator.ts b/jdl/validators/entity-validator.ts index c7522a66a867..3b4995b30d2d 100644 --- a/jdl/validators/entity-validator.ts +++ b/jdl/validators/entity-validator.ts @@ -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; @@ -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); + } } } diff --git a/jdl/validators/enum-validator.spec.ts b/jdl/validators/enum-validator.spec.ts index 3da9c20a4107..e28730af20af 100644 --- a/jdl/validators/enum-validator.spec.ts +++ b/jdl/validators/enum-validator.spec.ts @@ -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\.$/); }); }); diff --git a/jdl/validators/enum-validator.ts b/jdl/validators/enum-validator.ts index fa2a37f81142..a263f17bd4bd 100644 --- a/jdl/validators/enum-validator.ts +++ b/jdl/validators/enum-validator.ts @@ -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; @@ -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); + } } } diff --git a/jdl/validators/jdl-with-application-validator.ts b/jdl/validators/jdl-with-application-validator.ts index fc5ad293cbd9..66a7dbc2d768 100644 --- a/jdl/validators/jdl-with-application-validator.ts +++ b/jdl/validators/jdl-with-application-validator.ts @@ -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; @@ -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(); }); @@ -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; } @@ -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); }); } @@ -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); }); } diff --git a/jdl/validators/jdl-without-application-validator.ts b/jdl/validators/jdl-without-application-validator.ts index 813c20ce9680..822613b8b88c 100644 --- a/jdl/validators/jdl-without-application-validator.ts +++ b/jdl/validators/jdl-without-application-validator.ts @@ -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'; @@ -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; /** @@ -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)) { @@ -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); }); } diff --git a/jdl/validators/validator.ts b/jdl/validators/validator.ts index e15fa97c296b..0af4b3574a8f 100644 --- a/jdl/validators/validator.ts +++ b/jdl/validators/validator.ts @@ -17,6 +17,8 @@ * limitations under the License. */ +export type ValidatorOptions = { checkReservedKeywords?: boolean }; + export default class Validator { objectType: any; fieldsToCheck: any; @@ -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}.`); }