Skip to content

Commit 0c697d5

Browse files
authored
Support overriding input format in JS API (#237)
1 parent 91c9b7c commit 0c697d5

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

node/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const asset = fs.readFileSync(fullpath);
4848

4949
validator.validateBytes(new Uint8Array(asset), {
5050
uri: filename,
51+
format: 'gltf', // skip auto-detection and parse the input as glTF JSON
5152
maxIssues: 10, // limit max number of output issues to 10
5253
ignoredIssues: ['UNSUPPORTED_EXTENSION'], // mute UNSUPPORTED_EXTENSION issue
5354
severityOverrides: { 'ACCESSOR_INDEX_TRIANGLE_DEGENERATE': 0 }, // treat degenerate triangles as errors

node/gltf_validator.dart

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ abstract class Exports {
6363
abstract class _JSValidationOptions {
6464
external Object get uri;
6565

66+
external Object get format;
67+
6668
external ExternalResourceFunction get externalResourceFunction;
6769

6870
external bool get writeTimestamp;
@@ -116,13 +118,22 @@ Future<Map<String, Object>> validateBytes(
116118
final _options = _checkOptionsObject(options);
117119
final context = _getContextFromOptions(_options);
118120

119-
GltfReaderResult readerResult;
120-
try {
121-
final reader = await GltfReader.detect(Stream.value(data), context);
122-
readerResult = await reader.read();
123-
} on GltfInvalidFormatException {
124-
rethrow;
121+
GltfReader reader;
122+
switch ((_options?.format as String)?.toLowerCase()) {
123+
case 'glb':
124+
reader = GlbReader(Stream.value(data), context);
125+
break;
126+
case 'gltf':
127+
reader = GltfJsonReader(Stream.value(data), context);
128+
break;
129+
default:
130+
try {
131+
reader = await GltfReader.detect(Stream.value(data), context);
132+
} on GltfInvalidFormatException {
133+
rethrow;
134+
}
125135
}
136+
final readerResult = await reader.read();
126137

127138
return _validateResourcesAndGetReport(_options, context, readerResult);
128139
}
@@ -204,6 +215,10 @@ Context _getContextFromOptions(_JSValidationOptions options) {
204215
List<String> ignoredIssues;
205216

206217
if (options != null) {
218+
if (options.format != null && options.format is! String) {
219+
throw ArgumentError('options.format: Value must be a string.');
220+
}
221+
207222
if (options.maxIssues != null &&
208223
(options.maxIssues is! int || options.maxIssues < 0)) {
209224
throw ArgumentError(

node/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ exports.validateString = (json, options) => validator.validateString(json, optio
4747
/**
4848
@typedef {Object} ValidationOptions
4949
@property {string} uri - Absolute or relative asset URI that will be copied to validation report.
50+
@property {string} format - Set to `glb` or `gltf` to skip auto-detection of the asset format based on the first byte; any other value will be ignored. This option has no effect on `validateString`.
5051
@property {ExternalResourceFunction} externalResourceFunction - Function for loading external resources. If omitted, external resources are not validated.
5152
@property {boolean} writeTimestamp - Set to `false` to omit timestamp from the validation report. Default is `true`.
5253
@property {number} maxIssues - Max number of reported issues. Use `0` for unlimited output.

node/module.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const validateString = (json, options) => v.default.validateString(json,
4747
/**
4848
@typedef {Object} ValidationOptions
4949
@property {string} uri - Absolute or relative asset URI that will be copied to validation report.
50+
@property {string} format - Set to `glb` or `gltf` to skip auto-detection of the asset format based on the first byte; any other value will be ignored. This option has no effect on `validateString`.
5051
@property {ExternalResourceFunction} externalResourceFunction - Function for loading external resources. If omitted, external resources are not validated.
5152
@property {boolean} writeTimestamp - Set to `false` to omit timestamp from the validation report.
5253
@property {number} maxIssues - Max number of reported issues. Use `0` for unlimited output.

0 commit comments

Comments
 (0)