Skip to content

Commit

Permalink
feat(lyra): adds guards to language analyzer settings
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Jun 11, 2022
1 parent 748a745 commit 0fa4078
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
7 changes: 7 additions & 0 deletions packages/lyra/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SUPPORTED_LANGUAGES } from "./stemmer";

function formatJSON(input: object) {
return JSON.stringify(input, null, 2);
}
Expand All @@ -22,3 +24,8 @@ export const UNSUPPORTED_NESTED_PROPERTIES = () =>

export const DOC_ID_DOES_NOT_EXISTS = (id: string) =>
`Document with ID ${id} does not exists`;

export const LANGUAGE_NOT_SUPPORTED = (lang: string) =>
`Language "${lang}" is not supported.\nSupported languages are:\n - ${SUPPORTED_LANGUAGES.join(
"\n - "
)}`;
15 changes: 13 additions & 2 deletions packages/lyra/src/lyra.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Trie } from "./prefix-tree/trie";
import * as ERRORS from "./errors";
import { tokenize } from "./tokenizer";
import { formatNanoseconds, getNanosecondsTime } from "./utils";
import { Language } from "./stemmer";
import { Language, SUPPORTED_LANGUAGES } from "./stemmer";

export type PropertyType = "string" | "number" | "boolean";

Expand Down Expand Up @@ -53,7 +53,14 @@ export class Lyra {
);

constructor(properties: LyraProperties) {
this.defaultLanguage = properties.defaultLanguage || "english";
const defaultLanguage =
(properties?.defaultLanguage?.toLowerCase() as Language) ?? "english";

if (!SUPPORTED_LANGUAGES.includes(defaultLanguage)) {
throw ERRORS.LANGUAGE_NOT_SUPPORTED(defaultLanguage);
}

this.defaultLanguage = defaultLanguage;
this.schema = properties.schema;
this.buildIndex(properties.schema);
}
Expand Down Expand Up @@ -194,6 +201,10 @@ export class Lyra {
): Promise<{ id: string }> {
const id = nanoid();

if (!SUPPORTED_LANGUAGES.includes(language)) {
throw ERRORS.LANGUAGE_NOT_SUPPORTED(language);
}

if (!(await this.checkInsertDocSchema(doc))) {
throw ERRORS.INVALID_DOC_SCHEMA(this.schema, doc);
}
Expand Down
28 changes: 28 additions & 0 deletions packages/lyra/tests/__snapshots__/lyra.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,31 @@ but found the following doc:
}
}"
`;

exports[`defaultLanguage should throw an error if the desired language is not supported 1`] = `
"Language \\"latin\\" is not supported.
Supported languages are:
- dutch
- english
- french
- italian
- norwegian
- portugese
- russian
- spanish
- swedish"
`;

exports[`defaultLanguage should throw an error if the desired language is not supported during insertion 1`] = `
"Language \\"latin\\" is not supported.
Supported languages are:
- dutch
- english
- french
- italian
- norwegian
- portugese
- russian
- spanish
- swedish"
`;
43 changes: 43 additions & 0 deletions packages/lyra/tests/lyra.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,49 @@ function getId({ id }: { id: string }) {
return id;
}

describe("defaultLanguage", () => {
it("should throw an error if the desired language is not supported", () => {
try {
new Lyra({
schema: {},
// @ts-expect-error latin is not supported
defaultLanguage: "latin",
});
} catch (e) {
expect(e).toMatchSnapshot();
}
});

it("should throw an error if the desired language is not supported during insertion", async () => {
try {
const db = new Lyra({
schema: { foo: "string" },
});

await db.insert(
{
foo: "bar",
},
// @ts-expect-error latin is not supported
"latin"
);
} catch (e) {
expect(e).toMatchSnapshot();
}
});

it("should not throw if if the language is supported", () => {
try {
new Lyra({
schema: {},
defaultLanguage: "portugese",
});
} catch (e) {
expect(e).toBeUndefined();
}
});
});

describe("checkInsertDocSchema", () => {
it("should compare the inserted doc with the schema definition", async () => {
const db = new Lyra({
Expand Down

0 comments on commit 0fa4078

Please sign in to comment.