@bedrock-core/builder-validation 0.0.0-stage → 0.1.0
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/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +4 -2
- package/dist/package.json +11 -0
- package/dist/schemas/languages.schema.json +8 -0
- package/dist/src/index.d.ts +64 -0
- package/dist/src/index.js +197 -0
- package/package.json +41 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @bedrock-core/builder-validation
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 09203ce: Expose package-owned `./plugin` entrypoints through the shared build context and
|
|
8
|
+
plugin contracts. Declare public export paths explicitly and keep implementation
|
|
9
|
+
helpers private. Bundler extensions contribute compiler plugins, virtual modules,
|
|
10
|
+
resolution settings and tracked inputs with conservative cache reuse. Standalone
|
|
11
|
+
packages load using their own declared dependencies.
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- 59ca6b9: Accept single terrain texture paths alongside path arrays when validating with the pinned Mojang schema catalog.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 @bedrock-core contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @bedrock-core/builder-validation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Validate pack JSON against Mojang's Minecraft Bedrock schemas.
|
|
4
|
+
|
|
5
|
+
https://bedrock-core.drav.dev/docs/builder/api/validatePackContent
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema#",
|
|
3
|
+
"title": "Resource pack language catalog",
|
|
4
|
+
"description": "The locale array in texts/languages.json, as documented by the Bedrock Wiki text guide.",
|
|
5
|
+
"type": "array",
|
|
6
|
+
"items": { "type": "string", "minLength": 1 },
|
|
7
|
+
"uniqueItems": true
|
|
8
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** Manifest-classified pack directory whose final JSON files are checked. */
|
|
2
|
+
export interface ContentPack {
|
|
3
|
+
/** Configured pack ID used in diagnostics. */
|
|
4
|
+
id: string;
|
|
5
|
+
/** Pack directory to inspect. */
|
|
6
|
+
path: string;
|
|
7
|
+
/** Manifest classification used to select applicable document schemas. */
|
|
8
|
+
kind: 'behavior' | 'resource' | 'other';
|
|
9
|
+
}
|
|
10
|
+
/** Validation failure located by pack, file and JSON pointer. */
|
|
11
|
+
export interface ContentDiagnostic {
|
|
12
|
+
/** Configured pack ID. */
|
|
13
|
+
pack: string;
|
|
14
|
+
/** JSON file path relative to the pack directory, using forward slashes. */
|
|
15
|
+
file: string;
|
|
16
|
+
/** JSON pointer identifying the invalid value. */
|
|
17
|
+
pointer: string;
|
|
18
|
+
/** Syntax or schema validation explanation. */
|
|
19
|
+
message: string;
|
|
20
|
+
}
|
|
21
|
+
/** Per-file syntax/schema diagnostics and any unavailable-schema explanation. */
|
|
22
|
+
export interface ContentFileResult {
|
|
23
|
+
/** Configured pack ID. */
|
|
24
|
+
pack: string;
|
|
25
|
+
/** Checked JSON path relative to the pack directory, using forward slashes. */
|
|
26
|
+
file: string;
|
|
27
|
+
/** Applied schema URL when a matching catalog schema is available. */
|
|
28
|
+
schema?: string;
|
|
29
|
+
/** Reason a matching upstream schema could not be applied. */
|
|
30
|
+
schemaIssue?: string;
|
|
31
|
+
/** Syntax and schema errors for this file. */
|
|
32
|
+
diagnostics: ContentDiagnostic[];
|
|
33
|
+
}
|
|
34
|
+
/** Schema catalog version and validation coverage for all checked files. */
|
|
35
|
+
export interface ContentValidationResult {
|
|
36
|
+
/** Resolved Mojang schema catalog version. */
|
|
37
|
+
schemaVersion: string;
|
|
38
|
+
/** Per-file checks and schema coverage. */
|
|
39
|
+
files: ContentFileResult[];
|
|
40
|
+
}
|
|
41
|
+
/** Pack selection, schema catalog and optional validation-reuse integration. */
|
|
42
|
+
export interface ContentValidationOptions {
|
|
43
|
+
/** Final pack directories to validate. */
|
|
44
|
+
packs: readonly ContentPack[];
|
|
45
|
+
/** Custom installed Mojang schema catalog directory. */
|
|
46
|
+
schemaRoot?: string;
|
|
47
|
+
/** Cooperative validation cancellation. */
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
/** Reuse hook keyed by file, schema dependencies and validator revision; call validate on a miss. */
|
|
50
|
+
reuse?: (file: string, dependencies: readonly string[], revision: string, validate: () => Promise<ContentFileResult>) => Promise<ContentFileResult>;
|
|
51
|
+
}
|
|
52
|
+
/** Aggregated JSON validation failure with structured diagnostics. */
|
|
53
|
+
export declare class ContentValidationError extends Error {
|
|
54
|
+
/** Structured validation failures collected across checked files. */
|
|
55
|
+
readonly diagnostics: readonly ContentDiagnostic[];
|
|
56
|
+
/** Format the supplied structured diagnostics as one validation error. */
|
|
57
|
+
constructor(diagnostics: ContentDiagnostic[]);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Validate every JSON document; catalog matches additionally receive schema checks.
|
|
61
|
+
* @param options Project inputs, settings and optional integrations for this operation.
|
|
62
|
+
* @returns Schema catalog version and per-file coverage; invalid documents throw ContentValidationError.
|
|
63
|
+
*/
|
|
64
|
+
export declare function validatePackContent(options: ContentValidationOptions): Promise<ContentValidationResult>;
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { readFile, readdir, realpath } from 'node:fs/promises';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
import { Ajv } from 'ajv';
|
|
8
|
+
import { parse, printParseErrorCode } from 'jsonc-parser';
|
|
9
|
+
import picomatch from 'picomatch';
|
|
10
|
+
/** Aggregated JSON validation failure with structured diagnostics. */
|
|
11
|
+
export class ContentValidationError extends Error {
|
|
12
|
+
/** Structured validation failures collected across checked files. */
|
|
13
|
+
diagnostics;
|
|
14
|
+
/** Format the supplied structured diagnostics as one validation error. */
|
|
15
|
+
constructor(diagnostics) {
|
|
16
|
+
super(`Invalid pack content:\n${diagnostics.map(issue => `${issue.pack}/${issue.file}${issue.pointer}: ${issue.message}`).join('\n')}`);
|
|
17
|
+
this.name = 'ContentValidationError';
|
|
18
|
+
this.diagnostics = diagnostics;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function within(root, file) {
|
|
22
|
+
const relative = path.relative(root, file);
|
|
23
|
+
return relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Validate every JSON document; catalog matches additionally receive schema checks.
|
|
27
|
+
* @param options Project inputs, settings and optional integrations for this operation.
|
|
28
|
+
* @returns Schema catalog version and per-file coverage; invalid documents throw ContentValidationError.
|
|
29
|
+
*/
|
|
30
|
+
export async function validatePackContent(options) {
|
|
31
|
+
const require = createRequire(import.meta.url);
|
|
32
|
+
const root = await realpath(options.schemaRoot ?? path.dirname(require.resolve('@minecraft/bedrock-schemas/catalog.json')));
|
|
33
|
+
const catalogFile = path.join(root, 'catalog.json');
|
|
34
|
+
const catalog = JSON.parse(await readFile(catalogFile, 'utf8'));
|
|
35
|
+
const metadataFile = path.join(root, 'package.json');
|
|
36
|
+
const metadata = JSON.parse(await readFile(metadataFile, 'utf8'));
|
|
37
|
+
const validators = new Map();
|
|
38
|
+
const unavailable = new Map();
|
|
39
|
+
const documents = new Map();
|
|
40
|
+
const referencesByFile = new Map();
|
|
41
|
+
const ajv = new Ajv({ allErrors: true, strict: false, validateFormats: false, logger: false });
|
|
42
|
+
async function load(file, inputs) {
|
|
43
|
+
options.signal?.throwIfAborted();
|
|
44
|
+
file = await realpath(file);
|
|
45
|
+
if (!within(root, file)) {
|
|
46
|
+
throw new Error(`Schema reference must stay inside its package: ${file}`);
|
|
47
|
+
}
|
|
48
|
+
inputs.add(file);
|
|
49
|
+
const previous = documents.get(file);
|
|
50
|
+
if (previous) {
|
|
51
|
+
for (const reference of referencesByFile.get(file) ?? []) {
|
|
52
|
+
if (!inputs.has(reference)) {
|
|
53
|
+
await load(reference, inputs);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return previous;
|
|
57
|
+
}
|
|
58
|
+
const document = JSON.parse(await readFile(file, 'utf8'));
|
|
59
|
+
const references = new Set();
|
|
60
|
+
if (!options.schemaRoot && metadata.version === '1.26.50'
|
|
61
|
+
&& file === path.join(root, 'schemas/rp/textures/terrain_texture.schema.json')) {
|
|
62
|
+
// Vanilla terrain atlases use single texture paths as well as arrays.
|
|
63
|
+
// https://github.com/Mojang/bedrock-samples/blob/main/resource_pack/textures/terrain_texture.json
|
|
64
|
+
const properties = document.properties;
|
|
65
|
+
const entry = properties.texture_data.additionalProperties;
|
|
66
|
+
entry.properties.textures = { anyOf: [{ type: 'string' }, entry.properties.textures] };
|
|
67
|
+
}
|
|
68
|
+
function normalize(value) {
|
|
69
|
+
if (Array.isArray(value)) {
|
|
70
|
+
value.forEach(normalize);
|
|
71
|
+
}
|
|
72
|
+
else if (value && typeof value === 'object') {
|
|
73
|
+
const node = value;
|
|
74
|
+
delete node.$id;
|
|
75
|
+
if (typeof node.$ref === 'string' && !node.$ref.startsWith('#')) {
|
|
76
|
+
let target = new URL(node.$ref, pathToFileURL(file));
|
|
77
|
+
if (target.protocol !== 'file:') {
|
|
78
|
+
throw new Error(`Remote schema references are unsupported: ${target.href}`);
|
|
79
|
+
}
|
|
80
|
+
// The pinned Mojang block schemas use the legacy Molang object's alternate filename.
|
|
81
|
+
if (!options.schemaRoot && metadata.version === '1.26.50'
|
|
82
|
+
&& fileURLToPath(target) === path.join(root, 'schemas/common/expression.schema.json') && !existsSync(fileURLToPath(target))) {
|
|
83
|
+
const fragment = target.hash;
|
|
84
|
+
target = pathToFileURL(path.join(root, 'schemas/common/molang_expression.schema.json'));
|
|
85
|
+
target.hash = fragment;
|
|
86
|
+
}
|
|
87
|
+
node.$ref = target.href;
|
|
88
|
+
target.hash = '';
|
|
89
|
+
references.add(fileURLToPath(target));
|
|
90
|
+
}
|
|
91
|
+
Object.values(node).forEach(normalize);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
normalize(document);
|
|
95
|
+
document.$id = pathToFileURL(file).href;
|
|
96
|
+
documents.set(file, document);
|
|
97
|
+
referencesByFile.set(file, references);
|
|
98
|
+
for (const reference of references) {
|
|
99
|
+
await load(reference, inputs);
|
|
100
|
+
}
|
|
101
|
+
return document;
|
|
102
|
+
}
|
|
103
|
+
async function validator(file) {
|
|
104
|
+
const previous = validators.get(file);
|
|
105
|
+
if (previous) {
|
|
106
|
+
return previous;
|
|
107
|
+
}
|
|
108
|
+
const inputs = new Set([catalogFile, metadataFile]);
|
|
109
|
+
const document = await load(file, inputs);
|
|
110
|
+
for (const schema of documents.values()) {
|
|
111
|
+
// Register the complete graph before compiling cyclic or shared references.
|
|
112
|
+
if (!ajv.schemas[schema.$id]) {
|
|
113
|
+
ajv.addSchema(schema);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
const validate = ajv.getSchema(document.$id) ?? ajv.compile(document);
|
|
117
|
+
const result = { validate, inputs: [...inputs] };
|
|
118
|
+
validators.set(file, result);
|
|
119
|
+
return result;
|
|
120
|
+
}
|
|
121
|
+
const revision = createHash('sha256').update(await readFile(new URL(import.meta.url))).digest('hex');
|
|
122
|
+
const files = [];
|
|
123
|
+
for (const pack of options.packs) {
|
|
124
|
+
const packRoot = await realpath(pack.path);
|
|
125
|
+
const prefix = pack.kind === 'behavior' ? 'bp/' : pack.kind === 'resource' ? 'rp/' : undefined;
|
|
126
|
+
const entries = prefix ? catalog.schemas.filter(entry => entry.url.startsWith(`./schemas/${prefix}`)) : [];
|
|
127
|
+
async function visit(directory) {
|
|
128
|
+
for (const entry of (await readdir(directory, { withFileTypes: true })).sort((a, b) => a.name.localeCompare(b.name))) {
|
|
129
|
+
options.signal?.throwIfAborted();
|
|
130
|
+
const source = path.join(directory, entry.name);
|
|
131
|
+
const relative = path.relative(packRoot, source).replaceAll('\\', '/');
|
|
132
|
+
if (entry.isSymbolicLink()) {
|
|
133
|
+
throw new Error(`Pack content cannot use links: ${pack.id}/${relative}`);
|
|
134
|
+
}
|
|
135
|
+
if (entry.isDirectory()) {
|
|
136
|
+
await visit(source);
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (!entry.isFile() || !/\.json$/i.test(entry.name) || relative === 'manifest.json') {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
// Exact filenames take precedence over folder-wide catalog patterns.
|
|
143
|
+
const matches = entries.filter(item => item.fileMatch?.some(pattern => picomatch.isMatch(relative, pattern)));
|
|
144
|
+
matches.sort((a, b) => (a.fileMatch?.join().includes('/**/') ? 1 : 0) - (b.fileMatch?.join().includes('/**/') ? 1 : 0));
|
|
145
|
+
const selected = matches[0];
|
|
146
|
+
let compiled;
|
|
147
|
+
let schemaIssue = selected ? unavailable.get(selected.url) : undefined;
|
|
148
|
+
if (pack.kind === 'resource' && relative === 'texts/languages.json') {
|
|
149
|
+
const file = fileURLToPath(new URL('../schemas/languages.schema.json', import.meta.url));
|
|
150
|
+
compiled = { validate: ajv.compile(JSON.parse(await readFile(file, 'utf8'))), inputs: [file, catalogFile, metadataFile] };
|
|
151
|
+
}
|
|
152
|
+
else if (selected && !schemaIssue) {
|
|
153
|
+
try {
|
|
154
|
+
compiled = await validator(path.resolve(root, selected.url));
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
options.signal?.throwIfAborted();
|
|
158
|
+
// A broken upstream schema cannot establish content validity.
|
|
159
|
+
schemaIssue = `Schema unavailable: ${error instanceof Error ? error.message : String(error)}`;
|
|
160
|
+
unavailable.set(selected.url, schemaIssue);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const inputs = compiled?.inputs ?? [catalogFile, metadataFile];
|
|
164
|
+
async function check() {
|
|
165
|
+
const errors = [];
|
|
166
|
+
const document = parse(await readFile(source, 'utf8'), errors, { allowTrailingComma: true });
|
|
167
|
+
const diagnostics = errors.map(error => ({
|
|
168
|
+
pack: pack.id,
|
|
169
|
+
file: relative,
|
|
170
|
+
pointer: '',
|
|
171
|
+
message: `${printParseErrorCode(error.error)} at offset ${error.offset}`,
|
|
172
|
+
}));
|
|
173
|
+
if (!errors.length && compiled && !compiled.validate(document)) {
|
|
174
|
+
for (const error of compiled.validate.errors ?? []) {
|
|
175
|
+
diagnostics.push({ pack: pack.id, file: relative, pointer: error.instancePath, message: error.message ?? error.keyword });
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
pack: pack.id,
|
|
180
|
+
file: relative,
|
|
181
|
+
...(compiled && selected ? { schema: selected.url } : {}),
|
|
182
|
+
...(schemaIssue ? { schemaIssue } : {}),
|
|
183
|
+
diagnostics,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
const fileRevision = createHash('sha256').update(JSON.stringify({ revision, pack: pack.id, relative, schema: selected?.url, schemaIssue })).digest('hex');
|
|
187
|
+
files.push(await (options.reuse?.(source, inputs, fileRevision, check) ?? check()));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
await visit(packRoot);
|
|
191
|
+
}
|
|
192
|
+
const diagnostics = files.flatMap(file => file.diagnostics);
|
|
193
|
+
if (diagnostics.length) {
|
|
194
|
+
throw new ContentValidationError(diagnostics);
|
|
195
|
+
}
|
|
196
|
+
return { schemaVersion: metadata.version, files };
|
|
197
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bedrock-core/builder-validation",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Standalone pack content validation against Mojang's Bedrock schema catalog",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/src/index.d.ts",
|
|
9
|
+
"default": "./dist/src/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=22.18"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@minecraft/bedrock-schemas": "1.26.50",
|
|
17
|
+
"ajv": "8.17.1",
|
|
18
|
+
"jsonc-parser": "3.3.1",
|
|
19
|
+
"picomatch": "^4.0.2"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/picomatch": "^4.0.2"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist/**",
|
|
27
|
+
"./README.md",
|
|
28
|
+
"./LICENSE"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/src/index.d.ts",
|
|
34
|
+
"default": "./dist/src/index.js"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "git+https://github.com/bedrock-core/builder.git",
|
|
42
|
+
"directory": "packages/validation"
|
|
43
|
+
}
|
|
6
44
|
}
|