Skip to content

Commit

Permalink
feat: add validation of Thing Descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 5, 2022
1 parent 72c1002 commit 7af7429
Show file tree
Hide file tree
Showing 4 changed files with 822 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/definitions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
export 'src/definitions/form.dart';
export 'src/definitions/thing_description.dart';
export 'src/definitions/thing_model.dart';
export 'src/definitions/validation/thing_description_schema.dart'
show thingDescriptionSchema;
3 changes: 2 additions & 1 deletion lib/src/core/exposed_thing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ExposedThing implements scripting_api.ExposedThing {

/// Creates a new [ExposedThing] from a [servient] and an [exposedThingInit].
ExposedThing(this.servient, ExposedThingInit exposedThingInit)
: thingDescription = ThingDescription.fromJson(exposedThingInit) {
: thingDescription =
ThingDescription.fromJson(exposedThingInit, validate: false) {
title = thingDescription.title;
id = thingDescription.id;
}
Expand Down
9 changes: 8 additions & 1 deletion lib/src/definitions/thing_description.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'interaction_affordances/action.dart';
import 'interaction_affordances/event.dart';
import 'interaction_affordances/property.dart';
import 'link.dart';
import 'validation/thing_description_schema.dart';
import 'security/apikey_security_scheme.dart';
import 'security/basic_security_scheme.dart';
import 'security/bearer_security_scheme.dart';
Expand Down Expand Up @@ -110,7 +111,13 @@ class ThingDescription {
}

/// Creates a [ThingDescription] from a [json] object.
ThingDescription.fromJson(Map<String, dynamic> json) {
ThingDescription.fromJson(Map<String, dynamic> json, {bool validate = true}) {
if (validate) {
final validationResult = thingDescriptionSchema.validate(json);
if (!validationResult.isValid) {
throw ThingDescriptionValidationException(json);
}
}
_parseJson(json);
}

Expand Down
Loading

0 comments on commit 7af7429

Please sign in to comment.