Skip to content

Commit

Permalink
Consolidate Validation Helpers (#467)
Browse files Browse the repository at this point in the history
* consolidate validation

---------

Co-authored-by: E. Cooper <[email protected]>
  • Loading branch information
henryfauna and ecooper authored Dec 5, 2024
1 parent 05dec44 commit 4165d88
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 28 deletions.
9 changes: 2 additions & 7 deletions src/commands/database/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ import { container } from "../../cli.mjs";
import { throwForError } from "../../lib/fauna.mjs";
import { getSecret, retryInvalidCredsOnce } from "../../lib/fauna-client.mjs";
import { formatObjectForShell } from "../../lib/misc.mjs";
import { validateSecretOrDatabase } from "./database.mjs";

function validate(argv) {
validateSecretOrDatabase(argv);
return true;
}
import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs";

async function runCreateQuery(secret, argv) {
const { fql } = container.resolve("fauna");
Expand Down Expand Up @@ -80,7 +75,7 @@ function buildCreateCommand(yargs) {
description: "User-defined priority for the database.",
},
})
.check(validate)
.check(validateDatabaseOrSecret)
.help("help", "show help")
.example([
[
Expand Down
11 changes: 0 additions & 11 deletions src/commands/database/database.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ function buildDatabase(yargs) {
.help("help", "Show help.");
}

export function validateSecretOrDatabase(argv) {
// Makes sure the user has provided either a secret or a database so we can
// successfully authenticate them.
if (!argv.secret && !argv.database) {
throw new Error(
"No secret or database provided. Please use either --secret or --database.",
);
}
return true;
}

export default {
command: "database <method>",
aliases: ["db"],
Expand Down
9 changes: 2 additions & 7 deletions src/commands/database/delete.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import { FaunaError } from "fauna";
import { container } from "../../cli.mjs";
import { throwForError } from "../../lib/fauna.mjs";
import { getSecret, retryInvalidCredsOnce } from "../../lib/fauna-client.mjs";
import { validateSecretOrDatabase } from "./database.mjs";

function validate(argv) {
validateSecretOrDatabase(argv);
return true;
}
import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs";

async function runDeleteQuery(secret, argv) {
const { fql } = container.resolve("fauna");
Expand Down Expand Up @@ -53,7 +48,7 @@ function buildDeleteCommand(yargs) {
description: "Name of the database to delete.",
},
})
.check(validate)
.check(validateDatabaseOrSecret)
.help("help", "Show help.")
.example([
[
Expand Down
4 changes: 3 additions & 1 deletion src/lib/command-helpers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function isUnknownError(error) {
* Validate that the user has specified either a database or a secret.
* This check is not required for commands that can operate at a
* "root" level.
*
* @param {object} argv
* @param {string} argv.database - The database to use
* @param {string} argv.secret - The secret to use
Expand All @@ -151,9 +152,10 @@ export function isUnknownError(error) {
export const validateDatabaseOrSecret = (argv) => {
if (!argv.database && !argv.secret && !argv.local) {
throw new ValidationError(
"No database or secret specified. Pass either --database, or --secret, or --local.",
"No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.",
);
}
return true;
};

// used for queries customers can configure
Expand Down
2 changes: 1 addition & 1 deletion test/database/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("database create", () => {
{
command: "database create --name 'testdb'",
message:
"No secret or database provided. Please use either --secret or --database.",
"No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.",
},
].forEach(({ command, message }) => {
it(`validates invalid arguments: ${command}`, async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/database/delete.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("database delete", () => {
{
command: "database delete --name 'testdb'",
message:
"No secret or database provided. Please use either --secret or --database.",
"No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.",
},
].forEach(({ command, message }) => {
it(`validates invalid arguments: ${command}`, async () => {
Expand Down

0 comments on commit 4165d88

Please sign in to comment.