Skip to content

Commit

Permalink
Updating vaidation rules and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kshitij Tandon <tandonks@amazon.com>
  • Loading branch information
tandonks committed Oct 25, 2024
1 parent 29a3cee commit 9fddbc6
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
47 changes: 34 additions & 13 deletions server/routes/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ export default function (services: NodeServices, router: IRouter, dataSourceEnab
validate: {
params: schema.object({
index: schema.string({
pattern: /^[^A-Z-_"*+/\\|?#<>][^A-Z"*+/\\|?#<>]*$/,
minLength: 1,
maxLength: 100000,
validate: (value) => {
const invalidCharactersPattern = /[\s,:\"*+\/\\|?#><]/;
if (value !== value.toLowerCase() || value.startsWith("_") || value.startsWith("-") || invalidCharactersPattern.test(value)) {
return "Invalid index name.";
}

return undefined;
},
}),
}),
query: schema.object({
Expand All @@ -123,9 +128,7 @@ export default function (services: NodeServices, router: IRouter, dataSourceEnab
...(dataSourceEnabled
? {
dataSourceId: schema.string({
minLength: 1,
maxLength: 100000,
pattern: "^[a-zA-Z0-9_-]+$",
}),
}
: {}),
Expand Down Expand Up @@ -157,19 +160,39 @@ export default function (services: NodeServices, router: IRouter, dataSourceEnab
enabled_at: schema.maybe(schema.any()),
description: schema.maybe(schema.string()),
source_index: schema.string({
pattern: /^[^A-Z-_"*+/\\|?#<>][^A-Z"*+/\\|?#<>]*$/,
minLength: 1,
maxLength: 100000,
validate: (value) => {
const invalidCharactersPattern = /[\s,:\"*+\/\\|?#><]/;
if (
value !== value.toLowerCase() ||
value.startsWith("_") ||
value.startsWith("-") ||
invalidCharactersPattern.test(value)
) {
return "Invalid index name.";
}

return undefined;
},
}),
data_selection_query: schema.object({
match_all: schema.object({
boost: schema.maybe(schema.number()),
}),
}),
target_index: schema.string({
pattern: /^[^A-Z-_"*+/\\|?#<>][^A-Z"*+/\\|?#<>]*$/,
minLength: 1,
maxLength: 100000,
validate: (value) => {
const invalidCharactersPattern = /[\s,:\"*+\/\\|?#><]/;
if (
value !== value.toLowerCase() ||
value.startsWith("_") ||
value.startsWith("-") ||
invalidCharactersPattern.test(value)
) {
return "Invalid index name.";
}

return undefined;
},
}),
page_size: schema.number(),
groups: schema.arrayOf(
Expand All @@ -188,9 +211,7 @@ export default function (services: NodeServices, router: IRouter, dataSourceEnab
...(dataSourceEnabled
? {
dataSourceId: schema.string({
minLength: 1,
maxLength: 100000,
pattern: "^[a-zA-Z0-9_-]+$",
}),
}
: {}),
Expand Down
56 changes: 56 additions & 0 deletions server/services/TransformService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { schema } from "@osd/config-schema";

describe("Index Name Validation", () => {
const validateSchema = schema.object({
indexName: schema.string({
validate: (value) => {
const invalidCharactersPattern = /[\s,:\"*+\/\\|?#><]/;
if (value !== value.toLowerCase() || value.startsWith("_") || value.startsWith("-") || invalidCharactersPattern.test(value)) {
return "Invalid index name.";
}

return undefined;
},
}),
dataSourceId: schema.maybe(schema.string()),
});

const validateQuery = (indexName: string) => {
try {
validateSchema.validate({ indexName });
return undefined;
} catch (e) {
return e.message;
}
};

it("should fail validation for index names with uppercase letters", () => {
const errorMessage = validateQuery("IndexNameWithUppercase");
expect(errorMessage).toBe("[indexName]: Invalid index name.");
});

it("should fail validation for index names starting with an underscore", () => {
const errorMessage = validateQuery("_indexname");
expect(errorMessage).toBe("[indexName]: Invalid index name.");
});

it("should fail validation for index names starting with a hyphen", () => {
const errorMessage = validateQuery("-indexname");
expect(errorMessage).toBe("[indexName]: Invalid index name.");
});

it("should fail validation for index names containing invalid characters", () => {
const errorMessage = validateQuery("********************************");
expect(errorMessage).toBe("[indexName]: Invalid index name.");
});

it("should pass validation for valid index names", () => {
const errorMessage = validateQuery("valid_index-name123");
expect(errorMessage).toBeUndefined();
});

it("should fail validation for index names containing spaces", () => {
const errorMessage = validateQuery("invalid index");
expect(errorMessage).toBe("[indexName]: Invalid index name.");
});
});

0 comments on commit 9fddbc6

Please sign in to comment.