histoire
Version:
A fast story book powered by Vite
123 lines (122 loc) • 5.88 kB
JavaScript
import { manifestSchema, optionSchema } from './schema.js';
/** Accept only plain JSON objects; package code is never inspected or executed. */
function isRecord(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value) && [Object.prototype, null].includes(Object.getPrototypeOf(value));
}
/** Retain precise source paths even when invalid array items are removed. */
function reportIssues(diagnostics, prefix, error) {
for (const issue of error.issues) {
const path = [prefix, ...issue.path].filter(part => part !== '').join('.');
if (issue.code === 'unrecognized_keys') {
for (const key of issue.keys)
diagnostics.push({ path: [path, key].filter(Boolean).join('.'), message: 'Unknown field' });
}
else {
diagnostics.push({ path, message: issue.message });
}
}
}
/** Parse independently recoverable items without changing the caller's JSON. */
function parseItems(value, schema, path, diagnostics) {
return value.flatMap((item, index) => {
// A malformed optional relation must not discard otherwise useful descriptive metadata.
if (path === 'adds' && isRecord(item) && item.contribution !== undefined) {
const relation = manifestSchema.shape.adds.unwrap().element.shape.contribution.safeParse(item.contribution);
if (!relation.success) {
reportIssues(diagnostics, `${path}.${index}.contribution`, relation.error);
const description = { ...item };
delete description.contribution;
item = description;
}
}
const result = schema.safeParse(item);
if (result.success)
return [result.data];
reportIssues(diagnostics, `${path}.${index}`, result.error);
return [];
});
}
/** Remove duplicate/conflicting paths and invalid conditional dependencies. */
function validateOptions(value, diagnostics) {
const candidates = [];
for (const [index, item] of value.entries()) {
const result = optionSchema.safeParse(item);
if (!result.success) {
reportIssues(diagnostics, `config.options.${index}`, result.error);
continue;
}
const option = result.data;
if (candidates.some(({ option: previous }) => previous.key === option.key || previous.key.startsWith(`${option.key}.`) || option.key.startsWith(`${previous.key}.`))) {
diagnostics.push({ path: `config.options.${index}.key`, message: 'Duplicate or overlapping option path' });
continue;
}
candidates.push({ option, index });
}
// Resolve references until stable: dropping a dependency also invalidates its consumers.
let changed = true;
while (changed) {
changed = false;
for (let index = candidates.length - 1; index >= 0; index--) {
const candidate = candidates[index];
for (const [key, value] of Object.entries(candidate.option.when ?? {})) {
const dependency = candidates.find(item => item.option.key === key)?.option;
const matches = dependency && key !== candidate.option.key && optionSchema.safeParse({ ...dependency, default: value }).success;
if (!matches) {
diagnostics.push({ path: `config.options.${candidate.index}.when.${key}`, message: 'Unknown option or invalid condition value' });
candidates.splice(index, 1);
changed = true;
break;
}
}
}
}
return candidates.map(candidate => candidate.option);
}
/** Best-effort validation: drop invalid optional data, reject invalid required data. */
export function validateMarketplaceManifest(value) {
const diagnostics = [];
if (!isRecord(value))
return { manifest: undefined, diagnostics: [{ path: '', message: 'Expected a manifest object' }] };
const output = {};
let requiredInvalid = false;
for (const key of Object.keys(value)) {
if (!Object.hasOwn(manifestSchema.shape, key))
diagnostics.push({ path: key, message: 'Unknown field' });
}
for (const [key, schema] of Object.entries(manifestSchema.shape)) {
let input = value[key];
if (key === 'config' && isRecord(input)) {
const config = { import: input.import };
for (const field of Object.keys(input)) {
if (!Object.hasOwn(manifestSchema.shape.config.shape, field))
diagnostics.push({ path: `config.${field}`, message: 'Unknown field' });
}
if (Array.isArray(input.options)) {
config.options = validateOptions(input.options, diagnostics);
}
else if (input.options !== undefined) {
const result = manifestSchema.shape.config.shape.options.safeParse(input.options);
if (!result.success)
reportIssues(diagnostics, 'config.options', result.error);
}
input = config;
}
// Array members are independently useful, except a migration's operations are atomic.
if (Array.isArray(input) && 'unwrap' in schema) {
const unwrapped = schema.unwrap();
if ('element' in unwrapped)
input = parseItems(input, unwrapped.element, key, diagnostics);
}
const result = schema.safeParse(input);
if (result.success) {
if (result.data !== undefined)
output[key] = result.data;
}
else {
reportIssues(diagnostics, key, result.error);
if (!schema.isOptional())
requiredInvalid = true;
}
}
return { manifest: requiredInvalid ? undefined : output, diagnostics };
}