Skip to content

Commit

Permalink
Support overriding input format in JS API (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
lexaknyazev authored Oct 4, 2024
1 parent 91c9b7c commit 0c697d5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const asset = fs.readFileSync(fullpath);

validator.validateBytes(new Uint8Array(asset), {
uri: filename,
format: 'gltf', // skip auto-detection and parse the input as glTF JSON
maxIssues: 10, // limit max number of output issues to 10
ignoredIssues: ['UNSUPPORTED_EXTENSION'], // mute UNSUPPORTED_EXTENSION issue
severityOverrides: { 'ACCESSOR_INDEX_TRIANGLE_DEGENERATE': 0 }, // treat degenerate triangles as errors
Expand Down
27 changes: 21 additions & 6 deletions node/gltf_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ abstract class Exports {
abstract class _JSValidationOptions {
external Object get uri;

external Object get format;

external ExternalResourceFunction get externalResourceFunction;

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

GltfReaderResult readerResult;
try {
final reader = await GltfReader.detect(Stream.value(data), context);
readerResult = await reader.read();
} on GltfInvalidFormatException {
rethrow;
GltfReader reader;
switch ((_options?.format as String)?.toLowerCase()) {
case 'glb':
reader = GlbReader(Stream.value(data), context);
break;
case 'gltf':
reader = GltfJsonReader(Stream.value(data), context);
break;
default:
try {
reader = await GltfReader.detect(Stream.value(data), context);
} on GltfInvalidFormatException {
rethrow;
}
}
final readerResult = await reader.read();

return _validateResourcesAndGetReport(_options, context, readerResult);
}
Expand Down Expand Up @@ -204,6 +215,10 @@ Context _getContextFromOptions(_JSValidationOptions options) {
List<String> ignoredIssues;

if (options != null) {
if (options.format != null && options.format is! String) {
throw ArgumentError('options.format: Value must be a string.');
}

if (options.maxIssues != null &&
(options.maxIssues is! int || options.maxIssues < 0)) {
throw ArgumentError(
Expand Down
1 change: 1 addition & 0 deletions node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exports.validateString = (json, options) => validator.validateString(json, optio
/**
@typedef {Object} ValidationOptions
@property {string} uri - Absolute or relative asset URI that will be copied to validation report.
@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`.
@property {ExternalResourceFunction} externalResourceFunction - Function for loading external resources. If omitted, external resources are not validated.
@property {boolean} writeTimestamp - Set to `false` to omit timestamp from the validation report. Default is `true`.
@property {number} maxIssues - Max number of reported issues. Use `0` for unlimited output.
Expand Down
1 change: 1 addition & 0 deletions node/module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const validateString = (json, options) => v.default.validateString(json,
/**
@typedef {Object} ValidationOptions
@property {string} uri - Absolute or relative asset URI that will be copied to validation report.
@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`.
@property {ExternalResourceFunction} externalResourceFunction - Function for loading external resources. If omitted, external resources are not validated.
@property {boolean} writeTimestamp - Set to `false` to omit timestamp from the validation report.
@property {number} maxIssues - Max number of reported issues. Use `0` for unlimited output.
Expand Down

0 comments on commit 0c697d5

Please sign in to comment.