Skip to content

Commit

Permalink
fix(core): param prompts should not accept empty string for required …
Browse files Browse the repository at this point in the history
…properties (#15157)
  • Loading branch information
AgentEnder committed Feb 23, 2023
1 parent ca087ed commit cddfa09
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
32 changes: 29 additions & 3 deletions packages/nx/src/utils/params.spec.ts
Expand Up @@ -1501,6 +1501,7 @@ describe('params', () => {
'x-prompt': 'What is your name?',
},
},
required: ['name'],
},
{
version: 2,
Expand All @@ -1509,7 +1510,12 @@ describe('params', () => {
);

expect(prompts).toEqual([
{ type: 'input', name: 'name', message: 'What is your name?' },
{
type: 'input',
name: 'name',
message: 'What is your name?',
validate: expect.any(Function),
},
]);
});

Expand All @@ -1531,7 +1537,12 @@ describe('params', () => {
);

expect(prompts).toEqual([
{ type: 'confirm', name: 'isAwesome', message: 'Is this awesome?' },
{
type: 'confirm',
name: 'isAwesome',
message: 'Is this awesome?',
validate: expect.any(Function),
},
]);
});

Expand All @@ -1557,6 +1568,7 @@ describe('params', () => {
type: 'numeral',
name: 'age',
message: 'How old are you?',
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1589,6 +1601,7 @@ describe('params', () => {
name: 'pets',
message: 'What kind of pets do you have?',
choices: ['Cat', 'Dog', 'Fish'],
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1628,6 +1641,7 @@ describe('params', () => {
{ message: 'Dog', name: 'dog' },
{ message: 'Fish', name: 'fish' },
],
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1668,6 +1682,7 @@ describe('params', () => {
{ message: 'Dog', name: 'dog' },
{ message: 'Fish', name: 'fish' },
],
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1699,6 +1714,7 @@ describe('params', () => {
name: 'project',
message: 'Which project?',
choices: ['projA', 'projB'],
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1729,6 +1745,7 @@ describe('params', () => {
name: 'projectName',
message: 'Which project?',
choices: ['projA', 'projB'],
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1760,6 +1777,7 @@ describe('params', () => {
name: 'projectName',
message: 'Which project?',
choices: ['projA', 'projB'],
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1793,6 +1811,7 @@ describe('params', () => {
name: 'yourProject',
message: 'Which project?',
choices: ['projA', 'projB'],
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1822,6 +1841,7 @@ describe('params', () => {
name: 'name',
message: 'What is your name?',
choices: ['Bob', 'Joe', 'Jeff'],
validate: expect.any(Function),
},
]);
});
Expand Down Expand Up @@ -1852,6 +1872,7 @@ describe('params', () => {
message: 'What is your name?',
choices: ['Bob', 'Joe', 'Jeff'],
initial: 'Joe',
validate: expect.any(Function),
},
]);
});
Expand All @@ -1876,7 +1897,12 @@ describe('params', () => {
);

expect(prompts).toEqual([
{ type: 'input', name: 'name', message: 'What is your name?' },
{
type: 'input',
name: 'name',
message: 'What is your name?',
validate: expect.any(Function),
},
]);
});
});
Expand Down
20 changes: 13 additions & 7 deletions packages/nx/src/utils/params.ts
Expand Up @@ -747,13 +747,13 @@ function getGeneratorDefaults(
return defaults;
}

interface Prompt {
type Prompt = ConstructorParameters<typeof import('enquirer').Prompt>[0] & {
name: string;
type: 'input' | 'autocomplete' | 'multiselect' | 'confirm' | 'numeral';
message: string;
initial?: any;
choices?: (string | { name: string; message: string })[];
}
};

export function getPromptsForSchema(
opts: Options,
Expand Down Expand Up @@ -781,6 +781,14 @@ export function getPromptsForSchema(
}

question.message = v['x-prompt'].message;
question.validate = (s) => {
try {
validateProperty(k, s, schema, schema.definitions || {});
return true;
} catch (e) {
return e.message;
}
};

if (v.type === 'string' && v.enum && Array.isArray(v.enum)) {
question.type = 'autocomplete';
Expand All @@ -796,16 +804,13 @@ export function getPromptsForSchema(
question.type = 'autocomplete';
question.choices = Object.keys(projectsConfigurations.projects);
} else if (v.type === 'number' || v['x-prompt'].type == 'number') {
question.message = v['x-prompt'].message;
question.type = 'numeral';
} else if (
v['x-prompt'].type == 'confirmation' ||
v['x-prompt'].type == 'confirm'
) {
question.message = v['x-prompt'].message;
question.type = 'confirm';
} else if (v['x-prompt'].items) {
question.message = v['x-prompt'].message;
question.type =
v['x-prompt'].multiselect || v.type === 'array'
? 'multiselect'
Expand All @@ -822,9 +827,10 @@ export function getPromptsForSchema(
};
}
});
} else if (v.type === 'boolean') {
question.type = 'confirm';
} else {
question.message = v['x-prompt'].message;
question.type = v.type === 'boolean' ? 'confirm' : 'input';
question.type = 'input';
}
prompts.push(question);
}
Expand Down

1 comment on commit cddfa09

@vercel
Copy link

@vercel vercel bot commented on cddfa09 Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev
nx-dev-nrwl.vercel.app

Please sign in to comment.