Skip to content

Commit

Permalink
add required field for request form data
Browse files Browse the repository at this point in the history
  • Loading branch information
akashvichhi committed Jul 28, 2024
1 parent 6992aba commit 6c88f7e
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 6c88f7e

Please sign in to comment.