Skip to content

Commit

Permalink
v0.2.1: show optional properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kshyr committed May 16, 2024
1 parent 89ba7b9 commit 29ff7bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
openapi.json
types.js
.env
zig-cache
zig-out
21 changes: 20 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ fn build_jsdoc_typedef(allocator: std.mem.Allocator, schema_key: []const u8, sch
const type_ = try get_jsdoc_type(allocator, value, "");
try output.append(type_);
try output.append("} ");
try output.append(key);

if (is_property_required(schema_object, key)) {
try output.append(key);
} else {
try output.append("[");
try output.append(key);
try output.append("]");
}

try output.append("\n");
}
try output.append(" */\n");
Expand All @@ -47,6 +55,17 @@ fn build_jsdoc_typedef(allocator: std.mem.Allocator, schema_key: []const u8, sch
return output_str;
}

fn is_property_required(schema_object: std.json.Value, property_key: []const u8) bool {
const required = schema_object.object.get("required") orelse return false;

for (required.array.items) |required_key| {
if (std.mem.eql(u8, required_key.string, property_key)) {
return true;
}
}
return false;
}

fn get_jsdoc_type(allocator: std.mem.Allocator, value: std.json.Value, array_suffix: []const u8) ![]const u8 {
if (value.object.get("enum")) |enum_| {
const enum_values = enum_.array.items;
Expand Down

0 comments on commit 29ff7bc

Please sign in to comment.