Skip to content

Commit

Permalink
feat: add missing data schema fields
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Sep 22, 2022
1 parent 506d20b commit 629aafc
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 2 deletions.
115 changes: 114 additions & 1 deletion lib/src/definitions/data_schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,34 @@ void parseDataSchemaJson(DataSchema dataSchema, Map<String, dynamic> json) {
..readOnly = json.parseField<bool>('readOnly') ?? dataSchema.readOnly
..writeOnly = json.parseField<bool>('writeOnly') ?? dataSchema.writeOnly
..format = json.parseField<String>('format')
..type = json.parseField<String>('type');
..type = json.parseField<String>('type')
..minimum = json.parseField<num>('minimum')
..exclusiveMinimum = json.parseField<num>('exclusiveMinimum')
..maximum = json.parseField<num>('minimum')
..exclusiveMaximum = json.parseField<num>('exclusiveMaximum')
..multipleOf = json.parseField<num>('multipleOf')
..minItems = json.parseField<int>('minItems')
..maxItems = json.parseField<int>('maxItems')
..required = json.parseField<List<String>>('required')
..minLength = json.parseField<int>('minLength')
..maxLength = json.parseField<int>('maxLength')
..pattern = json.parseField<String>('pattern')
..contentEncoding = json.parseField<String>('contentEncoding')
..contentMediaType = json.parseField<String>('contentMediaType');

final oneOf = json['oneOf'];
if (oneOf is List<Map<String, dynamic>>) {
dataSchema.oneOf = oneOf.map(DataSchema.fromJson).toList();
}

final properties = json['properties'];
if (properties is Map<String, Map<String, dynamic>>) {
dataSchema.properties = Map.fromEntries(
properties.entries.map(
(entry) => MapEntry(entry.key, DataSchema.fromJson(entry.value)),
),
);
}
}

/// Metadata that describes the data format used. It can be used for validation.
Expand All @@ -27,6 +54,9 @@ void parseDataSchemaJson(DataSchema dataSchema, Map<String, dynamic> json) {
///
/// [spec link]: https://w3c.github.io/wot-thing-description/#dataschema
class DataSchema {
// TODO: Consider creating separate classes for each data type.
// Also see https://github.com/w3c/wot-thing-description/issues/1390

/// Creates a new [DataSchema] from a [json] object.
DataSchema.fromJson(Map<String, dynamic> json) {
parseDataSchemaJson(this, json);
Expand Down Expand Up @@ -82,6 +112,89 @@ class DataSchema {
/// or null.
String? type;

// Number/Integer fields

/// Specifies a minimum numeric value, representing an inclusive lower limit.
///
/// Only applicable for associated number or integer types.
num? minimum;

/// Specifies a minimum numeric value, representing an exclusive lower limit.
///
/// Only applicable for associated number or integer types.
num? exclusiveMinimum;

/// Specifies a maximum numeric value, representing an inclusive upper limit.
///
/// Only applicable for associated number or integer types.
num? maximum;

/// Specifies a maximum numeric value, representing an exclusive upper limit.
///
/// Only applicable for associated number or integer types.
num? exclusiveMaximum;

/// Specifies the multipleOf value number.
/// The value must strictly greater than 0.
///
/// Only applicable for associated number or integer types.
num? multipleOf;

// Array fields

/// Used to define the characteristics of an array.
List<DataSchema>? items;

/// Defines the minimum number of items that have to be in an array.
int? minItems;

/// Defines the maximum number of items that have to be in an array.
int? maxItems;

// Object fields

/// Data schema nested definitions in an `object`.
Map<String, DataSchema>? properties;

/// Defines which members of the `object` type are mandatory, i.e. which
/// members are mandatory in the payload that is to be sent (e.g. input of
/// invokeaction, writeproperty) and what members will be definitely delivered
/// in the payload that is being received (e.g. output of invokeaction,
/// readproperty).
List<String>? required;

// String fields

/// Specifies the minimum length of a string.
///
/// Only applicable for associated string types.
int? minLength;

/// Specifies the maximum length of a string.
///
/// Only applicable for associated string types.
int? maxLength;

/// Provides a regular expression to express constraints of the string value.
///
/// The regular expression must follow the [ECMA-262] dialect.
///
/// [ECMA-262]: https://tc39.es/ecma262/multipage/
String? pattern;

/// Specifies the encoding used to store the contents, as specified in
/// [RFC 2045] (Section 6.1) and [RFC 4648].
///
/// [RFC 2045]: https://www.rfc-editor.org/rfc/rfc2045
/// [RFC 4648]: https://www.rfc-editor.org/rfc/rfc4648
String? contentEncoding;

/// Specifies the MIME type of the contents of a string value, as described
/// in [RFC 2046].
///
/// [RFC 2046]: https://www.rfc-editor.org/rfc/rfc2046
String? contentMediaType;

/// The original JSON object that was parsed when creating this [DataSchema].
Map<String, dynamic>? rawJson;
}
45 changes: 45 additions & 0 deletions lib/src/definitions/interaction_affordances/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,51 @@ class Property extends InteractionAffordance implements DataSchema {
@override
bool? writeOnly = false;

@override
String? contentEncoding;

@override
String? contentMediaType;

@override
num? exclusiveMaximum;

@override
num? exclusiveMinimum;

@override
List<DataSchema>? items;

@override
int? maxItems;

@override
int? maxLength;

@override
num? maximum;

@override
int? minItems;

@override
int? minLength;

@override
num? minimum;

@override
num? multipleOf;

@override
String? pattern;

@override
Map<String, DataSchema>? properties;

@override
List<String>? required;

/// A hint that indicates whether Servients hosting the Thing and
/// Intermediaries should provide a Protocol Binding that supports the
/// `observeproperty` and `unobserveproperty` operations for this Property.
Expand Down
21 changes: 20 additions & 1 deletion test/core/definitions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:convert';
import 'package:dart_wot/dart_wot.dart';
import 'package:dart_wot/src/definitions/additional_expected_response.dart';
import 'package:dart_wot/src/definitions/context_entry.dart';
import 'package:dart_wot/src/definitions/data_schema.dart';
import 'package:dart_wot/src/definitions/expected_response.dart';
import 'package:dart_wot/src/definitions/interaction_affordances/interaction_affordance.dart';
import 'package:dart_wot/src/definitions/interaction_affordances/property.dart';
Expand Down Expand Up @@ -281,7 +282,16 @@ void main() {
'security': 'auto_sc',
}
],
}
},
'propertyWithOneOf': {
'oneOf': [
{'type': 'string'},
{'type': 'integer'}
],
'forms': [
{'href': 'https://example.org'}
]
},
}
};

Expand Down Expand Up @@ -321,6 +331,15 @@ void main() {
final testSchema = objectSchemeProperty?.properties?['test'];
expect(testSchema, isA<DataSchema>());
expect(testSchema?.type, 'string');
final propertyWithOneOf =
thingDescription.properties['propertyWithOneOf'];
final stringSchema = propertyWithOneOf?.oneOf?[0];
final integerSchema = propertyWithOneOf?.oneOf?[1];

expect(stringSchema, isA<DataSchema>());
expect(stringSchema?.type, 'string');
expect(integerSchema, isA<DataSchema>());
expect(integerSchema?.type, 'integer');
});
});

Expand Down

0 comments on commit 629aafc

Please sign in to comment.