@realnation/content-config 0.1.0 → 0.2.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/README.md CHANGED
@@ -12,7 +12,8 @@ AI 与团队建活动 skill 的读法:先从模块目录接口 `GET /partner/p
12
12
 
13
13
  ## 模块里怎么接
14
14
 
15
- 1. 装包:`npm install -D @realnation/content-config`
15
+ 1. 装包:`npm install -D @realnation/content-config`。esbuild 与 vite 是可选的同伴依赖(0.2.1 起):模块仓装了 vite 就带着 esbuild,
16
+ 不用另装;只用 `./validate` 的项目(如建活动 skill)两样都不用装。
16
17
  2. `vite.config.ts` 里加插件:
17
18
 
18
19
  ```ts
@@ -38,13 +39,18 @@ AI 与团队建活动 skill 的读法:先从模块目录接口 `GET /partner/p
38
39
  - **默认值与范围引用代码里的同一份常量**(默认值函数、LIMITS、枚举表),不照抄数字;改代码说明自动跟上。
39
40
  - **配置页不动**(PF 2026-09-29)。`x-label` 写配置页上显示的名字,一个字段在页上分几层时用「 · 」连起来
40
41
  (如 `Game Sounds · Grab`);**构建时每一段都要在模块 `src/` 的源码里原样找得到**,配置页改了名字而说明没跟上,
41
- 构建就报错。页上没有单独名字的字段(如随图片读回的宽高)不写 `x-label`。
42
+ 构建就报错。按页面上看到的样子写:模板里写成 HTML 转义的(如 `Playlists & Tracks`),`x-label` 写
43
+ `Playlists & Tracks`,核对时两种写法都认。页上没有单独名字的字段(如随图片读回的宽高)不写 `x-label`。
42
44
  - `description` 用英文写,给 AI 看:写清单位、取值的含义、空值与 0 代表什么;能用配置页上的提示就用它,不必一字不差。
43
45
  - **请求体按服务器格式写**:配置端内部与存库格式不同的(如 Personality Test 题目内部叫 questions、存时叫 details),
44
46
  写转换后的。
45
47
  - **空串会被存成 null**:后端全局有 `ConvertEmptyStringsToNull`(qik-sense-api `app/Http/Kernel.php`),
46
48
  允许空串的字段写 `type: ['string', 'null']`,并在说明里写清空串或 null 的含义。
47
49
  - `detail` 写 `additionalProperties: false` 并把默认值里的每个键都列进 `required`,默认值多出或少了字段构建就会报。
50
+ - **保存前必须填、出厂默认却是空的字段**(如 Video Player 叠层的视频:配置页新建时是空的,不传视频存不了):
51
+ 照实写约束(如 `minLength: 1`),再加 `'x-fill-before-save': true`。构建时校验出厂默认会放过它的空值
52
+ (空串、null 或空数组);skill 与 AI 校验真正要提交的请求体时照样拦住,AI 读到这个标注就知道这一格要自己填。
53
+ 不要为了让构建通过去放宽约束,也不要改产品的出厂默认。
48
54
  - 只支持这些关键字:type、properties、required、additionalProperties、items、minItems、maxItems、enum、const、
49
55
  minimum、maximum、minLength、maxLength、pattern、anyOf、format,另加 $schema、title、description、x-label。
50
56
  用到别的会被当成问题报出来。
@@ -52,6 +58,7 @@ AI 与团队建活动 skill 的读法:先从模块目录接口 `GET /partner/p
52
58
  ## 其他用法
53
59
 
54
60
  - `import { validate } from '@realnation/content-config/validate'`:返回问题清单,空数组 = 通过(skill 读到说明后用它校验要提交的请求体)。
61
+ 第四个参数 `{ allowUnfilled: true }` 只在校验出厂默认时用,会放过标了 `x-fill-before-save` 的空值。
55
62
  - `buildContentConfig(root, entry)`:不经 vite,直接读出并校验,返回要写进 `content-config.json` 的内容。
56
63
 
57
64
  ## 发版
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@realnation/content-config",
3
- "version": "0.1.0",
3
+ "version": "0.2.1",
4
4
  "description": "qiksense 模块内容配置的机器可读说明:构建时导出 content-config.json,并用字段说明校验默认请求体",
5
5
  "type": "module",
6
6
  "exports": {
@@ -20,12 +20,18 @@
20
20
  "scripts": {
21
21
  "test": "node --test \"test/*.test.mjs\""
22
22
  },
23
- "dependencies": {
24
- "esbuild": ">=0.19"
25
- },
26
23
  "peerDependencies": {
24
+ "esbuild": ">=0.19",
27
25
  "vite": ">=5"
28
26
  },
27
+ "peerDependenciesMeta": {
28
+ "esbuild": {
29
+ "optional": true
30
+ },
31
+ "vite": {
32
+ "optional": true
33
+ }
34
+ },
29
35
  "engines": {
30
36
  "node": ">=18"
31
37
  },
package/src/labels.mjs CHANGED
@@ -25,6 +25,17 @@ function* sourceFiles(dir) {
25
25
  }
26
26
  }
27
27
 
28
+ // 模板里的文字常写成 HTML 转义(如 Playlists & Tracks),页面上显示的是转义前的样子;
29
+ // x-label 写页面上看到的,所以核对时两种写法都认
30
+ const decodeEntities = (s) =>
31
+ s
32
+ .replace(/&/g, '&')
33
+ .replace(/&lt;/g, '<')
34
+ .replace(/&gt;/g, '>')
35
+ .replace(/&quot;/g, '"')
36
+ .replace(/&#39;|&apos;/g, "'")
37
+ .replace(/&nbsp;/g, ' ')
38
+
28
39
  export function checkLabels(schema, root, { dirs = ['src'], exclude = [] } = {}) {
29
40
  const excluded = new Set(exclude.map((f) => resolve(root, f)))
30
41
  let text = ''
@@ -33,6 +44,7 @@ export function checkLabels(schema, root, { dirs = ['src'], exclude = [] } = {})
33
44
  if (!excluded.has(resolve(file))) text += readFileSync(file, 'utf8') + '\n'
34
45
  }
35
46
  }
47
+ text += '\n' + decodeEntities(text)
36
48
  const problems = []
37
49
  for (const { path, label } of collectLabels(schema)) {
38
50
  for (const part of label.split(' · ')) {
package/src/validate.d.ts CHANGED
@@ -22,9 +22,16 @@ export interface JsonSchema {
22
22
  pattern?: string;
23
23
  anyOf?: ReadonlyArray<JsonSchema>;
24
24
  format?: string;
25
+ /** 保存前必须填、出厂默认可以留空(空串、null 或空数组)的字段;AI 与 skill 提交前要把它填好 */
26
+ 'x-fill-before-save'?: boolean;
25
27
  }
26
28
 
27
29
  export declare const SUPPORTED_KEYWORDS: ReadonlySet<string>;
28
30
 
31
+ export interface ValidateOptions {
32
+ /** 放过标了 x-fill-before-save 且还没填的字段。构建时校验出厂默认用;校验真正要提交的请求体时不要开 */
33
+ allowUnfilled?: boolean;
34
+ }
35
+
29
36
  /** 返回问题清单,空数组 = 通过 */
30
- export declare function validate(schema: JsonSchema, value: unknown, path?: string): string[];
37
+ export declare function validate(schema: JsonSchema, value: unknown, path?: string, options?: ValidateOptions): string[];
package/src/validate.mjs CHANGED
@@ -5,19 +5,25 @@
5
5
  export const SUPPORTED_KEYWORDS = new Set([
6
6
  '$schema', 'title', 'description', 'x-label', 'type', 'properties', 'required', 'additionalProperties',
7
7
  'items', 'minItems', 'maxItems', 'enum', 'const', 'minimum', 'maximum', 'minLength', 'maxLength', 'pattern',
8
- 'anyOf', 'format'
8
+ 'anyOf', 'format', 'x-fill-before-save'
9
9
  ])
10
10
 
11
+ // 值算不算「还没填」:空串、null、空数组。标了 x-fill-before-save 的字段,出厂默认可以是这样
12
+ const isUnfilled = (v) => v === '' || v === null || (Array.isArray(v) && v.length === 0)
13
+
11
14
  const typeOf = (v) => (v === null ? 'null' : Array.isArray(v) ? 'array' : Number.isInteger(v) ? 'integer' : typeof v)
12
15
  const matchesType = (v, t) => t === typeOf(v) || (t === 'number' && typeof v === 'number' && Number.isFinite(v))
13
16
  const same = (a, b) => JSON.stringify(a) === JSON.stringify(b)
14
17
 
15
- export function validate(schema, value, path = '$') {
18
+ // options.allowUnfilled:构建时校验出厂默认用,放过标了 x-fill-before-save 且还没填的字段;
19
+ // skill 校验真正要提交的请求体时不传,这类字段照样必须填好
20
+ export function validate(schema, value, path = '$', options = {}) {
16
21
  const errors = []
22
+ if (options.allowUnfilled && schema['x-fill-before-save'] === true && isUnfilled(value)) return errors
17
23
  for (const key of Object.keys(schema)) {
18
24
  if (!SUPPORTED_KEYWORDS.has(key)) errors.push(`${path}:字段说明用了不支持的关键字 ${key}`)
19
25
  }
20
- if (schema.anyOf && !schema.anyOf.some((s) => validate(s, value, path).length === 0)) {
26
+ if (schema.anyOf && !schema.anyOf.some((s) => validate(s, value, path, options).length === 0)) {
21
27
  errors.push(`${path}:${JSON.stringify(value)} 不符合 anyOf 里的任何一种`)
22
28
  }
23
29
  if (schema.type) {
@@ -43,7 +49,7 @@ export function validate(schema, value, path = '$') {
43
49
  if (Array.isArray(value)) {
44
50
  if (schema.minItems !== undefined && value.length < schema.minItems) errors.push(`${path}:${value.length} 项,少于 ${schema.minItems}`)
45
51
  if (schema.maxItems !== undefined && value.length > schema.maxItems) errors.push(`${path}:${value.length} 项,多于 ${schema.maxItems}`)
46
- if (schema.items) value.forEach((item, i) => errors.push(...validate(schema.items, item, `${path}[${i}]`)))
52
+ if (schema.items) value.forEach((item, i) => errors.push(...validate(schema.items, item, `${path}[${i}]`, options)))
47
53
  }
48
54
  if (typeOf(value) === 'object') {
49
55
  const props = schema.properties ?? {}
@@ -51,7 +57,7 @@ export function validate(schema, value, path = '$') {
51
57
  if (!(key in value)) errors.push(`${path}:缺少必填字段 ${key}`)
52
58
  }
53
59
  for (const [key, v] of Object.entries(value)) {
54
- if (props[key]) errors.push(...validate(props[key], v, `${path}.${key}`))
60
+ if (props[key]) errors.push(...validate(props[key], v, `${path}.${key}`, options))
55
61
  else if (schema.additionalProperties === false) errors.push(`${path}.${key}:字段说明里没有这个字段`)
56
62
  }
57
63
  }
@@ -2,7 +2,6 @@
2
2
  // 不通过就让构建失败;通过就在 remoteEntry.js 同一目录生成 content-config.json,随配置端产物发布。
3
3
  // 模块每次构建都跑(本机 npm run build、deploy.mjs 发 UAT 与正式),所以说明永远跟着当时的代码。
4
4
 
5
- import { build } from 'esbuild'
6
5
  import { mkdirSync, rmSync, writeFileSync } from 'node:fs'
7
6
  import { join, resolve } from 'node:path'
8
7
  import { pathToFileURL } from 'node:url'
@@ -13,9 +12,19 @@ export { checkLabels, collectLabels } from './labels.mjs'
13
12
 
14
13
  export const FORMAT = 1
15
14
 
15
+ // esbuild 是可选的同伴依赖(0.2.1 起):模块仓装了 vite 就有它;只用 ./validate 的场合(如建活动 skill)用不到,不必装
16
+ async function loadEsbuild() {
17
+ try {
18
+ return await import('esbuild')
19
+ } catch {
20
+ throw new Error('content-config:构建时要用 esbuild 加载 src/content-config.ts(vite 自带)。这个项目里找不到它,请 npm install -D esbuild')
21
+ }
22
+ }
23
+
16
24
  // 用 esbuild 把 content-config.ts 连同它引用的模块代码打成一个 ESM 文件再加载:TS、相对引用与 @/ 别名都能处理;
17
25
  // node_modules 里的包不打进去,加载时照常从模块仓解析(所以临时文件放在模块仓的 node_modules 下)
18
26
  export async function loadSpec(root, entry) {
27
+ const { build } = await loadEsbuild()
19
28
  const dir = join(root, 'node_modules', '.content-config')
20
29
  mkdirSync(dir, { recursive: true })
21
30
  const out = join(dir, `spec-${process.pid}-${Date.now()}.mjs`)
@@ -45,7 +54,8 @@ export async function buildContentConfig(root, entry = 'src/content-config.ts',
45
54
  throw new Error(`content-config:${entry} 要默认导出 { endpoint, defaultBody, schema }`)
46
55
  }
47
56
  const defaultBody = typeof spec.defaultBody === 'function' ? await spec.defaultBody() : spec.defaultBody
48
- const errors = validate(spec.schema, defaultBody)
57
+ // 出厂默认可以留空「保存前必须填」的字段(x-fill-before-save),其余照常校验
58
+ const errors = validate(spec.schema, defaultBody, '$', { allowUnfilled: true })
49
59
  if (labelDirs.length) errors.push(...checkLabels(spec.schema, root, { dirs: labelDirs, exclude: [entry] }))
50
60
  if (errors.length) {
51
61
  throw new Error(`content-config:字段说明没通过检查(${errors.length} 处)\n ${errors.join('\n ')}`)