Skip to content

Commit

Permalink
Merge pull request #113 from akashvichhi/feature/required-field-reque…
Browse files Browse the repository at this point in the history
…st-form-data

add required field for request form data
  • Loading branch information
ad-on-is authored Jul 30, 2024
2 parents 6992aba + 6c88f7e commit 55bf4bf
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export class CommentParser {

private parseRequestFormDataBody(rawLine: string) {
const line = rawLine.replace("@requestFormDataBody ", "");
let json = {};
let json = {}, required = [];
const isJson = isJSONString(line);
if (!isJson) {
// try to get json from reference
Expand All @@ -284,11 +284,13 @@ export class CommentParser {
let props = [];
const ref = this.exampleGenerator.schemas[cleandRef];
const ks = [];
if (ref.required && Array.isArray(ref.required)) required.push(...ref.required)
Object.entries(ref.properties).map(([key, value]) => {
if (typeof parsedRef[key] === "undefined") {
return;
}
ks.push(key);
if (value["required"]) required.push(key);
props.push({
[key]: {
type:
Expand All @@ -310,6 +312,11 @@ export class CommentParser {
}
} else {
json = JSON.parse(line);
for (let key in json) {
if (json[key].required === "true") {
required.push(key)
}
}
}
// No need to try/catch this JSON.parse as we already did that in the isJSONString function

Expand All @@ -319,6 +326,7 @@ export class CommentParser {
schema: {
type: "object",
properties: json,
required,
},
},
},
Expand Down Expand Up @@ -915,6 +923,7 @@ export class ValidatorParser {
: this.exampleGenerator.exampleByType("number"),
...meta,
};
if(!p["isOptinal"]) obj[p["fieldName"]]["required"] = true;
}
return obj;
}
Expand Down

0 comments on commit 55bf4bf

Please sign in to comment.