Skip to content

Commit

Permalink
Add anycase to ignore naming (#153)
Browse files Browse the repository at this point in the history
* Fix intellij meta data and configuration. WIP

* fix #148: Add a new `anyCase` naming convention to ignore everything. Also, finally removed the deprecated stuff since we have released at least 3 version since the deprecated warnings.

* Fix from code review
  • Loading branch information
JFCote authored Feb 24, 2021
1 parent b8f434b commit c1eb989
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 102 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,16 @@ The options file is described in json (example in `specs/options.json`), and has
|validateModelNoLocalDef|boolean|`true`, `false`|Not implemented yet|
|validateNaming|boolean|`true`, `false`|Ensures the names follow a given naming convention|
|ignoreHeaderXNaming|boolean|`true`, `false`|Exclude from validation header parameters starting with `x-`|
|pathNamingConvention|string|`CamelCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`|Naming convention for paths|
|parameterNamingConvention|string|`CamelCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`|Naming convention for parameters|
|headerNamingConvention|string|`CamelCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`|Naming convention for headers|
|propertyNamingConvention|string|`CamelCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`|Naming convention for properties|
|pathNamingConvention|string|`CamelCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase`|Naming convention for paths|
|parameterNamingConvention|string|`CamelCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase`|Naming convention for parameters|
|headerNamingConvention|string|`CamelCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase`|Naming convention for headers|
|propertyNamingConvention|string|`CamelCase`, `HyphenCase`, `UnderscoreCase`, `UnderscoreUpperCase`, `AnyCase`|Naming convention for properties|

## Roadmap

In no specific order

- Try to reach 100% unit test code coverage
- Make an official release
- Make it available on Maven
- Add some validations based on feedback

## Releases on maven central
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ private boolean isCamelCase(String variableName) {
return variableName.matches(REGEX_LOWER_CASE_ALPHA_NUMERIC_ONLY) || variableName.matches(REGEX_CAMEL_CASE);
}

private boolean isAnyCase() {
return true;
}

private boolean isHyphenCase(String variableName) {
return isSeparatorCaseValid(variableName, "-", false);
}



private boolean isSeparatorCaseValid(String variableName, String separator, boolean isUpperCase) {
if (variableName.startsWith(separator) || variableName.endsWith(separator)) {
Expand All @@ -37,15 +39,15 @@ private boolean isSeparatorCaseValid(String variableName, String separator, bool
}
totalLength += token.length();
if (isUpperCase) {
if (!token.toUpperCase().equals(token)) {
if (!token.equalsIgnoreCase(token)) {
return false;
}

if (!token.matches(REGEX_UPPER_CASE_ALPHA_NUMERIC_ONLY)) {
return false;
}
} else {
if (!token.toLowerCase().equals(token)) {
if (!token.equalsIgnoreCase(token)) {
return false;
}

Expand All @@ -68,6 +70,8 @@ boolean isNamingValid(String name, ValidatorParameters.NamingConvention namingSt
return isCamelCase(name);
case HyphenCase:
return isHyphenCase(name);
case AnyCase:
return isAnyCase();
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public static enum NamingConvention {
UnderscoreCase("underscore_case"),
UnderscoreUpperCase("UNDERSCORE_UPPER_CASE"),
CamelCase("camelCase"),
HyphenCase("hyphen-case");
HyphenCase("hyphen-case"),
AnyCase("AnyCase");

private final String designation;

Expand All @@ -41,40 +42,6 @@ public String getDesignation() {
}
}

/**
* @deprecated Please use {@link ValidatorParameters.NamingConvention} instead.
*/
@Deprecated
public static enum NamingStrategy {
UnderscoreCase,
UnderscoreUpperCase,
CamelCase,
HyphenCase;

/**
* @deprecated Please use {@link ValidatorParameters.NamingConvention#getDesignation()} instead.
* @return the name of the strategy as it can be used in the reports
*/
@Deprecated
public String getAppelation() {
return toConvention(this).getDesignation();
}

public static NamingStrategy valueOf(NamingConvention pathNamingConvention) {
if(pathNamingConvention == null) {
return null;
}
return NamingStrategy.valueOf(pathNamingConvention.name());
}

public static NamingConvention toConvention(NamingStrategy pathNamingStrategy) {
if(pathNamingStrategy == null) {
return null;
}
return NamingConvention.valueOf(pathNamingStrategy.name());
}
}

private boolean validateInfoLicense = true;
private boolean validateInfoDescription = true;
private boolean validateInfoContact = true;
Expand Down Expand Up @@ -155,22 +122,6 @@ public NamingConvention getPropertyNamingConvention() {
return propertyNamingConvention;
}

public NamingStrategy getPathNamingStrategy() {
return NamingStrategy.valueOf(getPathNamingConvention());
}

public NamingStrategy getParameterNamingStrategy() {
return NamingStrategy.valueOf(getParameterNamingConvention());
}

public NamingStrategy getHeaderNamingStrategy() {
return NamingStrategy.valueOf(getHeaderNamingConvention());
}

public NamingStrategy getPropertyNamingStrategy() {
return NamingStrategy.valueOf(getPropertyNamingConvention());
}

public ValidatorParameters setValidateInfoLicense(boolean validateInfoLicense) {
this.validateInfoLicense = validateInfoLicense;
return this;
Expand Down Expand Up @@ -241,26 +192,6 @@ public ValidatorParameters setPropertyNamingConvention(NamingConvention property
return this;
}

public ValidatorParameters setPathNamingStrategy(NamingStrategy pathNamingStrategy) {
setPathNamingConvention(NamingStrategy.toConvention(pathNamingStrategy));
return this;
}

public ValidatorParameters setParameterNamingStrategy(NamingStrategy parameterNamingStrategy) {
setParameterNamingConvention(NamingStrategy.toConvention(parameterNamingStrategy));
return this;
}

public ValidatorParameters setHeaderNamingStrategy(NamingStrategy headerNamingStrategy) {
setHeaderNamingConvention(NamingStrategy.toConvention(headerNamingStrategy));
return this;
}

public ValidatorParameters setPropertyNamingStrategy(NamingStrategy propertyNamingStrategy) {
setPropertyNamingConvention(NamingStrategy.toConvention(propertyNamingStrategy));
return this;
}

public boolean isValidateNaming() {
return validateNaming;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ void initEach() {
validator = new NamingValidator();
}

@Test
void anyCaseShouldAlwaysReturnTrue() {
//Arrange
String whateverCase1 = "my_variable";
String whateverCase2 = "my-variable";
String whateverCase3 = "variable";

//Act
boolean actual1 = validator.isNamingValid(whateverCase1, ValidatorParameters.NamingConvention.AnyCase);
boolean actual2 = validator.isNamingValid(whateverCase2, ValidatorParameters.NamingConvention.AnyCase);
boolean actual3 = validator.isNamingValid(whateverCase3, ValidatorParameters.NamingConvention.AnyCase);

//Assert
Assertions.assertAll(
() -> assertTrue(actual1),
() -> assertTrue(actual2),
() -> assertTrue(actual3)
);
}

@Test
void goodUnderscoreCaseShouldReturnTrue() {
//Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,4 @@ void testAllGetterSetters() {
() -> assertEquals(ValidatorParameters.NamingConvention.CamelCase, parameters.getPropertyNamingConvention())
);
}

@Test
void validateLegacyDefaultValuesAndSetters() {
Assertions.assertAll(
() -> assertEquals(ValidatorParameters.NamingStrategy.HyphenCase, parameters.getPathNamingStrategy()),
() -> assertEquals(ValidatorParameters.NamingStrategy.CamelCase, parameters.getParameterNamingStrategy()),
() -> assertEquals(ValidatorParameters.NamingStrategy.CamelCase, parameters.getPropertyNamingStrategy())
);

parameters.setPathNamingStrategy(ValidatorParameters.NamingStrategy.CamelCase);
parameters.setParameterNamingStrategy(ValidatorParameters.NamingStrategy.CamelCase);
parameters.setHeaderNamingStrategy(ValidatorParameters.NamingStrategy.UnderscoreUpperCase);
parameters.setPropertyNamingStrategy(ValidatorParameters.NamingStrategy.CamelCase);

Assertions.assertAll(
() -> assertEquals(ValidatorParameters.NamingConvention.CamelCase, parameters.getPathNamingConvention()),
() -> assertEquals(ValidatorParameters.NamingConvention.CamelCase, parameters.getParameterNamingConvention()),
() -> assertEquals(ValidatorParameters.NamingConvention.UnderscoreUpperCase, parameters.getHeaderNamingConvention()),
() -> assertEquals(ValidatorParameters.NamingConvention.CamelCase, parameters.getPropertyNamingConvention())
);
}
}

0 comments on commit c1eb989

Please sign in to comment.