Skip to content

Commit 0838740

Browse files
authored
[record_use] Run validation in Recordings.fromJson (dart-lang#3078)
dart-lang#3064 revealed we weren't ever running the validator. Refiling of: * dart-lang#3070
1 parent 9084e1d commit 0838740

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎pkgs/record_use/lib/src/recordings.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class Recordings {
4747
factory Recordings.fromJson(Map<String, Object?> json) {
4848
try {
4949
final syntax = RecordedUsesSyntax.fromJson(json);
50+
final syntaxErrors = syntax.validate();
51+
if (syntaxErrors.isNotEmpty) {
52+
final errorsString = syntaxErrors.map((e) => ' - $e').join('\n');
53+
throw FormatException(
54+
'Validation errors for record use file:\n$errorsString\n',
55+
);
56+
}
5057
return Recordings._fromSyntax(syntax);
5158
} on FormatException catch (e) {
5259
throw FormatException('''
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:record_use/record_use_internal.dart';
6+
import 'package:test/test.dart';
7+
8+
import '../test_data.dart';
9+
10+
void main() {
11+
group('Recordings.fromJson validation', () {
12+
test('Recordings.fromJson fails for invalid JSON', () {
13+
final json = recordedUses.toJson();
14+
// Modify the first recording's identifier URI to be an invalid type.
15+
final recordings = json['recordings'] as List;
16+
final recording = recordings[0] as Map;
17+
final identifier = recording['identifier'] as Map;
18+
identifier['uri'] = 123; // Should be a string
19+
20+
expect(
21+
() => Recordings.fromJson(json),
22+
throwsA(
23+
isA<FormatException>().having(
24+
(e) => e.message,
25+
'message',
26+
contains('Validation errors for record use file:'),
27+
),
28+
),
29+
);
30+
});
31+
});
32+
}

0 commit comments

Comments
 (0)