Skip to content

Commit

Permalink
Add onlyIssues validation option (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
Apidcloud authored Oct 18, 2024
1 parent 0c697d5 commit 1c107cd
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ max-issues: 10
ignore:
- UNEXPECTED_PROPERTY

# List of only issues to consider. Cannot be used along with 'ignore'.
only:
- ACCESSOR_INVALID_FLOAT

# Map of overridden severities. 0 - Error, 1 - Warning, 2 - Info, 3 - Hint
override:
ACCESSOR_INDEX_TRIANGLE_DEGENERATE: 0
18 changes: 18 additions & 0 deletions lib/cmd_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class ValidationTask {
ValidationOptions _getValidationOptionsFromYaml(String fileName) {
const kMaxIssues = 'max-issues';
const kIgnore = 'ignore';
const kOnly = 'only';
const kOverride = 'override';

void abort(Object e) {
Expand All @@ -111,6 +112,7 @@ ValidationOptions _getValidationOptionsFromYaml(String fileName) {

var maxIssues = 0;
List<String> ignoredIssues;
List<String> onlyIssues;
Map<String, Severity> severityOverrides;

if (yaml is Map) {
Expand All @@ -136,6 +138,21 @@ ValidationOptions _getValidationOptionsFromYaml(String fileName) {
abort("$kYamlError 'ignored' must be a sequence.");
}

final Object yamlOnlyIssues = yaml[kOnly];
if (yamlOnlyIssues is List) {
onlyIssues = List.generate(yamlOnlyIssues.length, (i) {
final Object entry = yamlOnlyIssues[i];
if (entry is String) {
return entry;
} else {
abort("$kYamlError each entry in '$kOnly' must be a string.");
return null;
}
}, growable: false);
} else if (yamlOnlyIssues != null) {
abort("$kYamlError 'only' must be a sequence.");
}

final Object yamlSeveritiesMap = yaml[kOverride];
if (yamlSeveritiesMap is Map) {
severityOverrides = <String, Severity>{};
Expand All @@ -156,6 +173,7 @@ ValidationOptions _getValidationOptionsFromYaml(String fileName) {
return ValidationOptions(
maxIssues: maxIssues,
ignoredIssues: ignoredIssues,
onlyIssues: onlyIssues,
severityOverrides: severityOverrides);
} else {
abort('$kYamlError document must be a map.');
Expand Down
14 changes: 13 additions & 1 deletion lib/src/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ import 'package:gltf/src/gl.dart' as gl;
class ValidationOptions {
final int maxIssues;
final Set<String> ignoredIssues = <String>{};
final Set<String> onlyIssues = <String>{};
final Map<String, Severity> severityOverrides;

ValidationOptions(
{int maxIssues, List<String> ignoredIssues, this.severityOverrides})
{int maxIssues,
List<String> ignoredIssues,
List<String> onlyIssues,
this.severityOverrides})
: maxIssues = maxIssues ?? 0 {
if (ignoredIssues != null) {
this.ignoredIssues.addAll(ignoredIssues);
}
if (onlyIssues != null) {
this.onlyIssues.addAll(onlyIssues);
}
}
}

Expand Down Expand Up @@ -220,6 +227,11 @@ class Context {
return;
}

if (options.onlyIssues.isNotEmpty &&
!options.onlyIssues.contains(issueType.code)) {
return;
}

if (options.maxIssues > 0 && _issues.length == options.maxIssues) {
_isTruncated = true;
throw const IssuesLimitExceededException();
Expand Down
1 change: 1 addition & 0 deletions node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ validator.validateBytes(new Uint8Array(asset), {
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
onlyIssues: ['ACCESSOR_INVALID_FLOAT'], // only consider ACCESSOR_INVALID_FLOAT an issue. Cannot be used along with ignoredIssues.
severityOverrides: { 'ACCESSOR_INDEX_TRIANGLE_DEGENERATE': 0 }, // treat degenerate triangles as errors
externalResourceFunction: (uri) =>
new Promise((resolve, reject) => {
Expand Down
26 changes: 26 additions & 0 deletions node/gltf_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ abstract class _JSValidationOptions {

external List get ignoredIssues;

external List get onlyIssues;

external Object get severityOverrides;
}

Expand Down Expand Up @@ -213,6 +215,7 @@ Context _getContextFromOptions(_JSValidationOptions options) {
ValidationOptions validationOptions;
Map<String, Severity> severityOverrides;
List<String> ignoredIssues;
List<String> onlyIssues;

if (options != null) {
if (options.format != null && options.format is! String) {
Expand All @@ -225,6 +228,28 @@ Context _getContextFromOptions(_JSValidationOptions options) {
'options.maxIssues: Value must be a non-negative integer.');
}

if (options.onlyIssues != null && options.ignoredIssues != null) {
throw ArgumentError(
'options.onlyIssues cannot be used along with options.ignoredIssues.');
}

if (options.onlyIssues != null) {
if (options.onlyIssues is! List) {
throw ArgumentError('options.onlyIssues: Value must be an array.');
}

onlyIssues = <String>[];
for (var i = 0; i < options.onlyIssues.length; ++i) {
final Object entry = options.onlyIssues[i];
if (entry is String && entry.isNotEmpty) {
onlyIssues.add(entry);
} else {
throw ArgumentError(
'options.onlyIssues[$i]: Value must be a non-empty String.');
}
}
}

if (options.ignoredIssues != null) {
if (options.ignoredIssues is! List) {
throw ArgumentError('options.ignoredIssues: Value must be an array.');
Expand Down Expand Up @@ -267,6 +292,7 @@ Context _getContextFromOptions(_JSValidationOptions options) {
validationOptions = ValidationOptions(
maxIssues: options.maxIssues,
ignoredIssues: ignoredIssues,
onlyIssues: onlyIssues,
severityOverrides: severityOverrides);
}

Expand Down
1 change: 1 addition & 0 deletions node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ exports.validateString = (json, options) => validator.validateString(json, optio
@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.
@property {string[]} ignoredIssues - Array of ignored issue codes.
@property {string[]} onlyIssues - Array of only issues to consider. Cannot be used along with ignoredIssues.
@property {Object} severityOverrides - Object with overridden severities for issue codes.
*/

Expand Down
1 change: 1 addition & 0 deletions node/module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const validateString = (json, options) => v.default.validateString(json,
@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.
@property {string[]} ignoredIssues - Array of ignored issue codes.
@property {string[]} onlyIssues - Array of only issues to consider. Cannot be used along with ignoredIssues.
@property {Object} severityOverrides - Object with overridden severities for issue codes.
*/

Expand Down

0 comments on commit 1c107cd

Please sign in to comment.