Fix validate config#878
Conversation
✅ Deploy Preview for livecodes ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
WalkthroughThe config validator now treats any object assigned to markup/style/script as an editor object and validates it via validateEditorProps. Tests were updated to expect a default language on script objects during validation. Changes
Sequence Diagram(s)sequenceDiagram
participant UserConfig as User Config
participant Validator as validate-config.ts
participant EditorProps as validateEditorProps
UserConfig->>Validator: validate(config)
alt field is object (markup/style/script)
Validator->>EditorProps: validateEditorProps(fieldObject)
EditorProps-->>Validator: normalized object (e.g., adds default language)
Validator-->>UserConfig: config with validated field
else field not object
Validator-->>UserConfig: config unchanged for that field
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
|
Size Change: -159 B (-0.02%) Total Size: 955 kB ℹ️ View Unchanged
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
src/livecodes/config/validate-config.ts (1)
57-76: Guardlanguagebefore callinggetLanguageEditorIdWith the broader object acceptance,
x.languagemay be missing. Guard to avoid passingundefinedintogetLanguageEditorId.- const validateEditorProps = (x: Editor, editorId: EditorId): Editor => ({ - language: fixSfcLanguage( - getLanguageEditorId(fixSfcLanguage(x.language, editorId)) === editorId - ? getLanguageByAlias(x.language) || defaultConfig[editorId].language - : defaultConfig[editorId].language, - editorId, - ), + const validateEditorProps = (x: Editor, editorId: EditorId): Editor => ({ + language: (() => { + const base = is((x as any).language, 'string') + ? ((x as any).language as Language) + : defaultConfig[editorId].language; + const adjusted = + getLanguageEditorId(fixSfcLanguage(base, editorId)) === editorId + ? getLanguageByAlias(base) || defaultConfig[editorId].language + : defaultConfig[editorId].language; + return fixSfcLanguage(adjusted, editorId); + })(), ...(is(x.title, 'string') ? { title: x.title } : {}), ...(is(x.content, 'string') ? { content: x.content } : {}), ...(is(x.contentUrl, 'string') ? { contentUrl: x.contentUrl } : {}), ...(is(x.hideTitle, 'boolean') ? { hideTitle: x.hideTitle } : {}), ...(is(x.hiddenContent, 'string') ? { hiddenContent: x.hiddenContent } : {}),src/livecodes/config/__tests__/validate-config.spec.ts (1)
160-160: Expectation update looks good; add array-case coverageThe new expectation aligns with broader object handling. Add a test to ensure arrays are rejected (after tightening the guard):
test('editor: invalid - array', () => { const testConfig = { script: [] } as any; const correctConfig = {}; expect(validateConfig(testConfig)).toEqual(correctConfig); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/livecodes/config/__tests__/validate-config.spec.ts(1 hunks)src/livecodes/config/validate-config.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/livecodes/config/validate-config.ts (1)
src/sdk/models.ts (1)
Editor(1093-1166)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (12)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Redirect rules - livecodes
- GitHub Check: Header rules - livecodes
- GitHub Check: build (18.x)
- GitHub Check: tests (18.x, 4)
- GitHub Check: tests (18.x, 5)
- GitHub Check: tests (18.x, 3)
- GitHub Check: tests (18.x, 1)
- GitHub Check: tests (18.x, 2)
- GitHub Check: Pages changed - livecodes
- GitHub Check: build
- GitHub Check: Cloudflare Pages
| ...(is(config.markup, 'object') | ||
| ? { markup: validateEditorProps(config.markup as Editor, 'markup') } | ||
| : {}), | ||
| ...(isEditor(config.style) | ||
| ...(is(config.style, 'object') | ||
| ? { style: validateEditorProps(config.style as Editor, 'style') } | ||
| : {}), | ||
| ...(isEditor(config.script) | ||
| ...(is(config.script, 'object') | ||
| ? { script: validateEditorProps(config.script as Editor, 'script') } | ||
| : {}), |
There was a problem hiding this comment.
Don’t treat arrays as editors; gate with a plain-object check
is(x, 'object') returns true for arrays, so markup/style/script arrays will be passed to validateEditorProps, silently coercing bad inputs into defaulted editors. Tighten the guard to exclude arrays.
Apply this diff:
@@
const zoomLevels: Array<Config['zoom']> = [1, 0.5, 0.25];
const isFoldedLines = (x: any) => is(x, 'object') && (is(x.from, 'number') || is(x.to, 'number'));
+ const isPlainObject = (x: any): x is Record<string, unknown> =>
+ x != null && typeof x === 'object' && !Array.isArray(x);
+
@@
- ...(is(config.markup, 'object')
+ ...(isPlainObject(config.markup)
? { markup: validateEditorProps(config.markup as Editor, 'markup') }
: {}),
- ...(is(config.style, 'object')
+ ...(isPlainObject(config.style)
? { style: validateEditorProps(config.style as Editor, 'style') }
: {}),
- ...(is(config.script, 'object')
+ ...(isPlainObject(config.script)
? { script: validateEditorProps(config.script as Editor, 'script') }
: {}),📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| ...(is(config.markup, 'object') | |
| ? { markup: validateEditorProps(config.markup as Editor, 'markup') } | |
| : {}), | |
| ...(isEditor(config.style) | |
| ...(is(config.style, 'object') | |
| ? { style: validateEditorProps(config.style as Editor, 'style') } | |
| : {}), | |
| ...(isEditor(config.script) | |
| ...(is(config.script, 'object') | |
| ? { script: validateEditorProps(config.script as Editor, 'script') } | |
| : {}), | |
| ...(isPlainObject(config.markup) | |
| ? { markup: validateEditorProps(config.markup as Editor, 'markup') } | |
| : {}), | |
| ...(isPlainObject(config.style) | |
| ? { style: validateEditorProps(config.style as Editor, 'style') } | |
| : {}), | |
| ...(isPlainObject(config.script) | |
| ? { script: validateEditorProps(config.script as Editor, 'script') } | |
| : {}), |
🤖 Prompt for AI Agents
In src/livecodes/config/validate-config.ts around lines 141 to 149, the current
guards use is(x, 'object') which returns true for arrays and allows arrays to be
passed into validateEditorProps; change the checks to ensure markup/style/script
are plain objects (exclude arrays and null) before calling validateEditorProps —
e.g., replace the is(...) checks with a plain-object test (like typeof ===
'object' && !Array.isArray(...) && value !== null or use a project helper such
as isPlainObject) so arrays are not treated as editors.



Summary by CodeRabbit