@localness/contract 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -71,6 +71,7 @@ export declare const deliveryKeyConventionSchema: z.ZodObject<{
71
71
  style: z.ZodEnum<{
72
72
  snake_case: "snake_case";
73
73
  camelCase: "camelCase";
74
+ none: "none";
74
75
  }>;
75
76
  maxDepth: z.ZodNumber;
76
77
  guidance: z.ZodNullable<z.ZodString>;
@@ -105,6 +106,7 @@ export declare const deliveryMetaSchema: z.ZodObject<{
105
106
  style: z.ZodEnum<{
106
107
  snake_case: "snake_case";
107
108
  camelCase: "camelCase";
109
+ none: "none";
108
110
  }>;
109
111
  maxDepth: z.ZodNumber;
110
112
  guidance: z.ZodNullable<z.ZodString>;
@@ -440,6 +442,7 @@ export declare const keyViolationSchema: z.ZodEnum<{
440
442
  prefix: "prefix";
441
443
  charset: "charset";
442
444
  segment: "segment";
445
+ separator: "separator";
443
446
  case: "case";
444
447
  depth: "depth";
445
448
  }>;
@@ -460,6 +463,7 @@ export declare const keyNameSuggestionSchema: z.ZodObject<{
460
463
  prefix: "prefix";
461
464
  charset: "charset";
462
465
  segment: "segment";
466
+ separator: "separator";
463
467
  case: "case";
464
468
  depth: "depth";
465
469
  }>>;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as KEY_STYLE_VALUES } from "./key-convention-C4ZHbNKy.mjs";
1
+ import { i as KEY_VIOLATION_VALUES, t as KEY_STYLE_VALUES } from "./key-convention-C-urfySC.mjs";
2
2
  import { z } from "zod/v4";
3
3
  // ../../shared/contract/release.ts
4
4
  /** A row id, which the API sends as a number but the UI also carries as text. */
@@ -303,13 +303,7 @@ const keyDraftConflictSchema = z.object({
303
303
  locales: z.array(z.string()),
304
304
  key: deliveryKeyStateSchema
305
305
  });
306
- const keyViolationSchema = z.enum([
307
- "prefix",
308
- "charset",
309
- "segment",
310
- "case",
311
- "depth"
312
- ]);
306
+ const keyViolationSchema = z.enum(KEY_VIOLATION_VALUES);
313
307
  /**
314
308
  * `POST /api/v1/keys/suggest-name` — the web app's AI key naming, under the
315
309
  * project's key convention. `key` is empty when the text has nothing nameable.
@@ -0,0 +1,108 @@
1
+ // ../../shared/contract/key-convention.ts
2
+ /**
3
+ * The per-project rules an i18n key has to follow. Pure, so a client that
4
+ * names keys itself checks them exactly the way the server does.
5
+ */
6
+ const KEY_STYLE_VALUES = [
7
+ "snake_case",
8
+ "camelCase",
9
+ "none"
10
+ ];
11
+ const KEY_VIOLATION_VALUES = [
12
+ "prefix",
13
+ "charset",
14
+ "segment",
15
+ "separator",
16
+ "case",
17
+ "depth"
18
+ ];
19
+ /**
20
+ * Errors make a key unusable as a name; warnings are the project's style, which
21
+ * legacy keys may never have followed, so nothing refuses a key over them.
22
+ */
23
+ const KEY_VIOLATION_LEVEL = {
24
+ prefix: "error",
25
+ charset: "error",
26
+ segment: "error",
27
+ separator: "warning",
28
+ case: "warning",
29
+ depth: "warning"
30
+ };
31
+ const KEY_VIOLATION_TEXT = {
32
+ prefix: "Missing the project key prefix",
33
+ charset: "Uses characters other than letters, digits, \".\", \"_\" and \"-\"",
34
+ segment: "Has an empty segment",
35
+ separator: "Uses a separator other than the configured one",
36
+ case: "Does not match the configured casing",
37
+ depth: "Has more levels than the convention suggests"
38
+ };
39
+ function keyViolationErrors(violations) {
40
+ return violations.filter((one) => KEY_VIOLATION_LEVEL[one] === "error");
41
+ }
42
+ function keyViolationWarnings(violations) {
43
+ return violations.filter((one) => KEY_VIOLATION_LEVEL[one] === "warning");
44
+ }
45
+ /** Characters any key may use to join its parts, whatever the configured separator. */
46
+ const KEY_BOUNDARY = [
47
+ ".",
48
+ "_",
49
+ "-"
50
+ ];
51
+ const WORD_PATTERN = {
52
+ snake_case: /^[a-z0-9]+$/,
53
+ camelCase: /^[a-z][A-Za-z0-9]*$/
54
+ };
55
+ function boundaryChars(separator) {
56
+ return /* @__PURE__ */ new Set([...KEY_BOUNDARY, ...separator]);
57
+ }
58
+ /**
59
+ * The characters that start a new level. In a snake_case project whose
60
+ * separator is not `_`, the underscore joins words inside one level.
61
+ */
62
+ function levelChars(convention) {
63
+ const chars = boundaryChars(convention.separator);
64
+ if (convention.style === "snake_case" && !convention.separator.includes("_")) chars.delete("_");
65
+ return chars;
66
+ }
67
+ function splitOn(text, chars) {
68
+ const parts = [""];
69
+ for (const char of text) if (chars.has(char)) parts.push("");
70
+ else parts[parts.length - 1] += char;
71
+ return parts;
72
+ }
73
+ /** How many levels a key path has under the convention. */
74
+ function keyDepth(path, convention) {
75
+ return path ? splitOn(path, levelChars(convention)).length : 0;
76
+ }
77
+ /**
78
+ * Pure: every way `key` breaks the convention, in the order worth fixing. Use
79
+ * `KEY_VIOLATION_LEVEL` to tell what must block from what is only advice.
80
+ *
81
+ * An empty key is **legal** (a symbol-only source text has no name), so it is
82
+ * not a violation here — callers treat it as "cannot name", not as an error.
83
+ *
84
+ * The prefix itself is exempt: it is copied verbatim from the project settings,
85
+ * so the configured rules govern the path that follows it. Any boundary
86
+ * character may follow it, so legacy keys that joined it differently still count.
87
+ */
88
+ function checkI18nKey(key, convention) {
89
+ if (!key) return [];
90
+ const { prefix, separator, style, maxDepth } = convention;
91
+ if (!separator) return [];
92
+ const boundary = boundaryChars(separator);
93
+ const violations = [];
94
+ const prefixed = Boolean(prefix) && key.startsWith(prefix) && boundary.has(key.charAt(prefix.length));
95
+ if (prefix && !prefixed) violations.push("prefix");
96
+ const path = prefixed ? key.slice(prefix.length + 1) : key;
97
+ const allowed = (char) => /[0-9a-zA-Z]/.test(char) || boundary.has(char);
98
+ if ([...path].some((char) => !allowed(char))) violations.push("charset");
99
+ if (splitOn(path, boundary).some((word) => word === "")) violations.push("segment");
100
+ const levels = levelChars(convention);
101
+ if ([...path].some((char) => levels.has(char) && !separator.includes(char))) violations.push("separator");
102
+ if (style !== "none") {
103
+ if (splitOn(path, boundary).some((word) => word && !WORD_PATTERN[style].test(word))) violations.push("case");
104
+ }
105
+ if (keyDepth(path, convention) > maxDepth) violations.push("depth");
106
+ return violations;
107
+ }
108
+ export { checkI18nKey as a, keyViolationWarnings as c, KEY_VIOLATION_VALUES as i, KEY_VIOLATION_LEVEL as n, keyDepth as o, KEY_VIOLATION_TEXT as r, keyViolationErrors as s, KEY_STYLE_VALUES as t };
package/dist/rules.d.mts CHANGED
@@ -2,28 +2,41 @@
2
2
  * The per-project rules an i18n key has to follow. Pure, so a client that
3
3
  * names keys itself checks them exactly the way the server does.
4
4
  */
5
- export declare const KEY_STYLE_VALUES: readonly ["snake_case", "camelCase"];
5
+ export declare const KEY_STYLE_VALUES: readonly ["snake_case", "camelCase", "none"];
6
6
  export type KeyStyle = (typeof KEY_STYLE_VALUES)[number];
7
7
  export type KeyConvention = {
8
8
  /** Without the trailing separator; `''` means no prefix. */
9
9
  prefix: string;
10
- /** Joins the segments. Must be non-empty — the settings schema enforces it. */
10
+ /** The suggested joint between levels. Must be non-empty — the settings schema enforces it. */
11
11
  separator: string;
12
+ /** `none` leaves casing to the project, for keys named before the convention existed. */
12
13
  style: KeyStyle;
13
- /** How many segments may follow the prefix. */
14
+ /** The suggested number of levels after the prefix. */
14
15
  maxDepth: number;
15
16
  };
17
+ export declare const KEY_VIOLATION_VALUES: readonly ["prefix", "charset", "segment", "separator", "case", "depth"];
16
18
  /** Why a key does not follow the convention. Sent to the client as a code. */
17
- export type KeyViolation = 'prefix' | 'charset' | 'segment' | 'case' | 'depth';
19
+ export type KeyViolation = (typeof KEY_VIOLATION_VALUES)[number];
20
+ /**
21
+ * Errors make a key unusable as a name; warnings are the project's style, which
22
+ * legacy keys may never have followed, so nothing refuses a key over them.
23
+ */
24
+ export declare const KEY_VIOLATION_LEVEL: Record<KeyViolation, 'error' | 'warning'>;
18
25
  export declare const KEY_VIOLATION_TEXT: Record<KeyViolation, string>;
26
+ export declare function keyViolationErrors(violations: KeyViolation[]): KeyViolation[];
27
+ export declare function keyViolationWarnings(violations: KeyViolation[]): KeyViolation[];
28
+ /** How many levels a key path has under the convention. */
29
+ export declare function keyDepth(path: string, convention: KeyConvention): number;
19
30
  /**
20
- * Pure: every way `key` breaks the convention, in the order worth fixing.
31
+ * Pure: every way `key` breaks the convention, in the order worth fixing. Use
32
+ * `KEY_VIOLATION_LEVEL` to tell what must block from what is only advice.
21
33
  *
22
34
  * An empty key is **legal** (a symbol-only source text has no name), so it is
23
35
  * not a violation here — callers treat it as "cannot name", not as an error.
24
36
  *
25
37
  * The prefix itself is exempt: it is copied verbatim from the project settings,
26
- * so the configured rules govern the path that follows it.
38
+ * so the configured rules govern the path that follows it. Any boundary
39
+ * character may follow it, so legacy keys that joined it differently still count.
27
40
  */
28
41
  export declare function checkI18nKey(key: string, convention: KeyConvention): KeyViolation[];
29
42
  /**
package/dist/rules.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { n as KEY_VIOLATION_TEXT, r as checkI18nKey, t as KEY_STYLE_VALUES } from "./key-convention-C4ZHbNKy.mjs";
1
+ import { a as checkI18nKey, c as keyViolationWarnings, i as KEY_VIOLATION_VALUES, n as KEY_VIOLATION_LEVEL, o as keyDepth, r as KEY_VIOLATION_TEXT, s as keyViolationErrors, t as KEY_STYLE_VALUES } from "./key-convention-C-urfySC.mjs";
2
2
  // \0rolldown/runtime.js
3
3
  var __create = Object.create;
4
4
  var __defProp = Object.defineProperty;
@@ -695,4 +695,4 @@ function entryMatchesReleaseFilter(releaseIds, filter) {
695
695
  if (filter === "unassigned") return ids.length === 0;
696
696
  return ids.some((releaseId) => String(releaseId) === String(filter));
697
697
  }
698
- export { KEY_STYLE_VALUES, KEY_VIOLATION_TEXT, LEGACY_DRAFT_KEY_PREFIX, NAMESPACE_BOUNDARY, UNNAMED_KEY_PREFIX, checkI18nKey, entryMatchesReleaseFilter, formatI18nKeyDisplay, fpTranslation, isUnnamedKey, namespaceOf, stripProjectPrefix };
698
+ export { KEY_STYLE_VALUES, KEY_VIOLATION_LEVEL, KEY_VIOLATION_TEXT, KEY_VIOLATION_VALUES, LEGACY_DRAFT_KEY_PREFIX, NAMESPACE_BOUNDARY, UNNAMED_KEY_PREFIX, checkI18nKey, entryMatchesReleaseFilter, formatI18nKeyDisplay, fpTranslation, isUnnamedKey, keyDepth, keyViolationErrors, keyViolationWarnings, namespaceOf, stripProjectPrefix };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@localness/contract",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Response contracts of the Localness public /api/v1: zod schemas and the types inferred from them.",
5
5
  "license": "Elastic-2.0",
6
6
  "type": "module",
@@ -1,49 +0,0 @@
1
- // ../../shared/contract/key-convention.ts
2
- /**
3
- * The per-project rules an i18n key has to follow. Pure, so a client that
4
- * names keys itself checks them exactly the way the server does.
5
- */
6
- const KEY_STYLE_VALUES = ["snake_case", "camelCase"];
7
- const KEY_VIOLATION_TEXT = {
8
- prefix: "Missing the project key prefix",
9
- charset: "Uses characters the key convention does not allow",
10
- segment: "Has an empty segment",
11
- case: "Does not match the configured casing",
12
- depth: "Has more segments than the convention allows"
13
- };
14
- const SEGMENT_PATTERN = {
15
- snake_case: /^[a-z0-9]+$/,
16
- camelCase: /^[a-z][A-Za-z0-9]*$/
17
- };
18
- /** The key an empty prefix produces nothing in front of. */
19
- function requiredStart(convention) {
20
- return convention.prefix ? convention.prefix + convention.separator : "";
21
- }
22
- /**
23
- * Pure: every way `key` breaks the convention, in the order worth fixing.
24
- *
25
- * An empty key is **legal** (a symbol-only source text has no name), so it is
26
- * not a violation here — callers treat it as "cannot name", not as an error.
27
- *
28
- * The prefix itself is exempt: it is copied verbatim from the project settings,
29
- * so the configured rules govern the path that follows it.
30
- */
31
- function checkI18nKey(key, convention) {
32
- if (!key) return [];
33
- const { separator, style, maxDepth } = convention;
34
- if (!separator) return [];
35
- const violations = [];
36
- const start = requiredStart(convention);
37
- const prefixed = Boolean(start) && key.startsWith(start);
38
- if (start && !prefixed) violations.push("prefix");
39
- const path = prefixed ? key.slice(start.length) : key;
40
- const separatorChars = new Set(separator.split(""));
41
- const allowed = (char) => /[0-9a-zA-Z]/.test(char) || separatorChars.has(char);
42
- if ([...path].some((char) => !allowed(char))) violations.push("charset");
43
- const segments = path.split(separator);
44
- if (segments.some((segment) => segment === "")) violations.push("segment");
45
- if (segments.length > maxDepth) violations.push("depth");
46
- if (segments.some((one) => one && !SEGMENT_PATTERN[style].test(one))) violations.push("case");
47
- return violations;
48
- }
49
- export { KEY_VIOLATION_TEXT as n, checkI18nKey as r, KEY_STYLE_VALUES as t };