Skip to content

Commit

Permalink
fix: sort properties by required
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Apr 8, 2022
1 parent 764b95f commit 75b250c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
22 changes: 22 additions & 0 deletions libs/server/src/lib/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ describe('utils', () => {
expect(r.map((x) => x.name)).toEqual(['a', 'c', 'b']);
});

it('should sort required arguments', async () => {
const r = await normalizeSchema({
properties: {
a: { $default: { $source: 'argv', index: 1 } },
b: {},
c: {},
d: { $default: { $source: 'argv', index: 0 } },
e: {},
},
required: ['c', 'e'],
});
expect(r.map((x) => x.name)).toMatchInlineSnapshot(`
Array [
"d",
"a",
"c",
"e",
"b",
]
`);
});

it('should set items when enum is provided', async () => {
const option = {
...mockOption,
Expand Down
6 changes: 3 additions & 3 deletions libs/server/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ export async function normalizeSchema(
return -1;
} else if (typeof b.positional === 'number') {
return 1;
} else if (a.required) {
if (b.required) {
} else if (a.isRequired) {
if (b.isRequired) {
return a.name.localeCompare(b.name);
}
return -1;
} else if (b.required) {
} else if (b.isRequired) {
return 1;
} else if (IMPORTANT_FIELDS_SET.has(a.name)) {
if (IMPORTANT_FIELDS_SET.has(b.name)) {
Expand Down

0 comments on commit 75b250c

Please sign in to comment.