Skip to content

Commit 19d4f43

Browse files
committed
feat: add a parseSchema function to convert a JSON string to SchemaType
1 parent 088039e commit 19d4f43

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

packages/dogs_core/lib/dogs_schema.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
library;
1818

19+
import "dart:convert";
20+
1921
import "dogs_core.dart";
2022

2123
/// Creates a schema type representing a string.
@@ -59,6 +61,15 @@ SchemaType enumeration(List<String> values) {
5961
return SchemaType.string.property(SchemaProperties.$enum, values);
6062
}
6163

64+
/// Parses a JSON string into a [SchemaType].
65+
SchemaType parseSchema(String json) {
66+
final map = jsonDecode(json);
67+
if (map is! Map<String, dynamic>) {
68+
throw ArgumentError("Invalid schema JSON: $json");
69+
}
70+
return SchemaType.fromProperties(map);
71+
}
72+
6273
/// Extension methods for SchemaType to provide a fluent API for schema definitions.
6374
extension SchemaTypeExtension on SchemaType {
6475
/// Makes this schema type an array of itself.

0 commit comments

Comments
 (0)