Skip to content

Commit

Permalink
getPropertyPath
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Jul 16, 2024
1 parent 9cc23ac commit 95decbc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare module "@cloudflare/json-schema-walker" {
schema: import("json-schema").JSONSchema7 | boolean,
path: string[],
parent?: import("json-schema").JSONSchema7,
parentPath?: string[],
parentPath: string[],
) => void;

/** Modifies in place */
Expand Down
20 changes: 9 additions & 11 deletions src/corpus/config/schema-transform.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { JSONSchema7 } from "json-schema";
import { schemaWalk, type Visitor } from "@cloudflare/json-schema-walker";
import { useI18n } from "vue-i18n";
import { capitalize } from "lodash";
import capitalize from "lodash/capitalize";
import useLocale from "@/i18n/locale.composable";

const isPropertyName = (name: string) =>
!/^[0-9]*$/.test(name) &&
!["properties", "allOf", "anyOf", "if", "then", "else", "not"].includes(name);
const getPropertyPath = (parts: string[]) =>
parts.filter((value, index) => parts[index - 1] == "properties");

/** Convert "foo_bar_baz" to "Foo bar baz" */
const prettyName = (name: string): string =>
capitalize(name.replace(/_/g, " "));

export function useTransformSchema() {
const { t } = useI18n();
const { te } = useLocale();

/** Set translated, human-readable titles and descriptions. */
function transformSchema(schema: JSONSchema7) {
schemaWalk(schema, undefined, postFunc);
}
Expand All @@ -24,9 +28,7 @@ export function useTransformSchema() {
const name = path[1];

// Construct path as string
const pathStr = [...(parentPath || []), name]
.filter(isPropertyName)
.join(".");
const pathStr = getPropertyPath([...parentPath, ...path]).join(".");

// Build translation keys
const titleKey = `config.schema.${pathStr}.title`;
Expand All @@ -38,9 +40,5 @@ export function useTransformSchema() {
if (te(descrKey)) schema.description = t(descrKey);
};

/** Convert "foo_bar_baz" to "Foo bar baz" */
const prettyName = (name: string): string =>
capitalize(name.replace(/_/g, " "));

return { transformSchema };
}

0 comments on commit 95decbc

Please sign in to comment.