Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anycase to ignore naming #153

Merged
merged 9 commits into from
Feb 24, 2021
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,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 @@ -24,7 +24,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 @@ -40,40 +41,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 @@ -149,22 +116,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 @@ -230,26 +181,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 @@ -73,25 +73,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())
);
}
}