Skip to content

Commit

Permalink
[typescript-fetch] Add option to generate validation attributes of mo…
Browse files Browse the repository at this point in the history
…del properties (#19448)
  • Loading branch information
matthiasschwarz authored Aug 27, 2024
1 parent aae3ab3 commit 4330b2f
Show file tree
Hide file tree
Showing 22 changed files with 3,047 additions and 1 deletion.
6 changes: 6 additions & 0 deletions bin/configs/typescript-fetch-validation-attributes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
generatorName: typescript-fetch
outputDir: samples/client/petstore/typescript-fetch/builds/validation-attributes
inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript-fetch/validation-attributes.yaml
templateDir: modules/openapi-generator/src/main/resources/typescript-fetch
additionalProperties:
validationAttributes: true
1 change: 1 addition & 0 deletions docs/generators/typescript-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|supportsES6|Generate code that conforms to ES6.| |false|
|useSingleRequestParameter|Setting this property to true will generate functions with a single argument containing all API endpoint parameters instead of one argument per parameter.| |true|
|useSquareBracketsInArrayNames|Setting this property to true will add brackets to array attribute names, e.g. my_values[].| |false|
|validationAttributes|Setting this property to true will generate the validation attributes of model properties.| |false|
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
|withoutRuntimeChecks|Setting this property to true will remove any runtime checks on the request and response payloads. Payloads will be casted to their expected types.| |false|

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
public static final String CAMEL_CASE = "camelCase";
public static final String PASCAL_CASE = "PascalCase";
public static final String USE_SQUARE_BRACKETS_IN_ARRAY_NAMES = "useSquareBracketsInArrayNames";
public static final String VALIDATION_ATTRIBUTES = "validationAttributes";

@Getter @Setter
protected String npmRepository = null;
Expand Down Expand Up @@ -92,7 +93,8 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
@Getter @Setter
protected String inferEntityFromUniqueIdWithName = null;
@Setter protected boolean packageAsSourceOnlyLibrary = false;

@Getter @Setter
protected Boolean generateValidationAttributes = false;

public TypeScriptFetchClientCodegen() {
super();
Expand Down Expand Up @@ -123,6 +125,7 @@ public TypeScriptFetchClientCodegen() {
this.cliOptions.add(new CliOption(IMPORT_FILE_EXTENSION_SWITCH, IMPORT_FILE_EXTENSION_SWITCH_DESC).defaultValue(""));
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'PascalCase', 'camelCase', 'kebab-case'.").defaultValue(this.fileNaming));
this.cliOptions.add(new CliOption(USE_SQUARE_BRACKETS_IN_ARRAY_NAMES, "Setting this property to true will add brackets to array attribute names, e.g. my_values[].", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(VALIDATION_ATTRIBUTES, "Setting this property to true will generate the validation attributes of model properties.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
}

@Override
Expand Down Expand Up @@ -308,6 +311,8 @@ public void processOpts() {
}
}
}

setGenerateValidationAttributes(convertPropertyToBooleanAndWriteBack(VALIDATION_ATTRIBUTES));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,68 @@ export function {{classname}}ToJSON(value?: {{#hasReadOnly}}Omit<{{classname}},
return value;
{{/hasVars}}
}
{{#validationAttributes}}

export const {{classname}}PropertyValidationAttributesMap: {
[property: string]: {
maxLength?: number,
minLength?: number,
pattern?: string,
maximum?: number,
exclusiveMaximum?: boolean,
minimum?: number,
exclusiveMinimum?: boolean,
multipleOf?: number,
maxItems?: number,
minItems?: number,
uniqueItems?: boolean
}
} = {
{{#vars}}
{{#hasValidation}}
{{name}}: {
{{#maxLength}}
maxLength: {{maxLength}},
{{/maxLength}}
{{#minLength}}
minLength: {{minLength}},
{{/minLength}}
{{#pattern}}
pattern: '{{pattern}}',
{{/pattern}}
{{#maximum}}
maximum: {{maximum}},
exclusiveMaximum: {{exclusiveMaximum}},
{{/maximum}}
{{#minimum}}
minimum: {{minimum}},
exclusiveMinimum: {{exclusiveMinimum}},
{{/minimum}}
{{#multipleOf}}
multipleOf: {{multipleOf}},
{{/multipleOf}}
{{#maxItems}}
maxItems: {{maxItems}},
{{/maxItems}}
{{#minItems}}
minItems: {{minItems}},
{{/minItems}}
{{#isArray}}
uniqueItems: {{uniqueItems}},
{{/isArray}}
},
{{/hasValidation}}
{{/vars}}
}
{{#isAdditionalPropertiesTrue}}

export const {{classname}}AdditionalPropertiesValidationAttributes: { maxProperties?: number, minProperties?: number } = {
{{#maxProperties}}
maxProperties: {{maxProperties}},
{{/maxProperties}}
{{#minProperties}}
minProperties: {{minProperties}},
{{/minProperties}}
}
{{/isAdditionalPropertiesTrue}}
{{/validationAttributes}}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public Map<String, String> createOptions() {
.put(CodegenConstants.ENUM_UNKNOWN_DEFAULT_CASE, ENUM_UNKNOWN_DEFAULT_CASE_VALUE)
.put(TypeScriptFetchClientCodegen.STRING_ENUMS, STRING_ENUMS)
.put(TypeScriptFetchClientCodegen.USE_SQUARE_BRACKETS_IN_ARRAY_NAMES, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.VALIDATION_ATTRIBUTES, Boolean.FALSE.toString())
.build();
}

Expand Down
Loading

0 comments on commit 4330b2f

Please sign in to comment.