simpleEncodeDecode function
dynamic
simpleEncodeDecode()
Implementation
dynamic simpleEncodeDecode(
Map<String, Object> typeTable,
String path,
String type,
Object? value,
) {
if (simpleStringTypes.contains(type)) {
if (value is! String) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
}
switch (type) {
case 'json':
return jsonDecode(jsonEncode(value, toEncodable: (Object? obj) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${obj.runtimeType}');
}));
case 'bool':
if (value is! bool) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
case 'hex':
if (value is! String ||
!RegExp(r'^(?:[A-Fa-f0-9]{2})*$').hasMatch(value)) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
case 'uuid':
if (value is! String ||
!RegExp(r'^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$')
.hasMatch(value)) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
case 'base64':
if (value is! String ||
Base64Encoder().convert(Base64Decoder().convert(value)) != value) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
case 'int':
if (value is! int) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
case 'uint':
if (value is! int || value < 0) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
case 'float':
if (value is! double && value is! int) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
case 'money':
if (value is! int) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return value;
case 'url':
if (value is! String || Uri.tryParse(value) == null) {
throw SdkgenTypeException(
'Invalid Type at \'$path\', expected $type, got ${jsonEncode(value)}');
}
return Uri.parse(value).toString();
case 'void':
return null;
}
throw SdkgenTypeException('Unknown type \'$type\' at \'$path\'');
}