Browse Source

dynamite: Support minLength and maxLength

pull/199/head
jld3103 2 years ago
parent
commit
93befad334
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 6
      packages/dynamite/lib/src/models/schema.dart
  2. 8
      packages/dynamite/lib/src/models/schema.g.dart
  3. 26
      packages/dynamite/lib/src/openapi_builder.dart

6
packages/dynamite/lib/src/models/schema.dart

@ -24,6 +24,8 @@ class Schema {
this.contentSchema,
this.discriminator,
this.pattern,
this.minLength,
this.maxLength,
});
factory Schema.fromJson(final Map<String, dynamic> json) => _$SchemaFromJson(json);
@ -73,6 +75,10 @@ class Schema {
final String? pattern;
final int? minLength;
final int? maxLength;
bool get isContentString => type == 'string' && (contentMediaType?.isNotEmpty ?? false) && contentSchema != null;
}

8
packages/dynamite/lib/src/models/schema.g.dart

@ -27,7 +27,9 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
'contentMediaType',
'contentSchema',
'discriminator',
'pattern'
'pattern',
'minLength',
'maxLength'
],
);
return Schema(
@ -53,6 +55,8 @@ Schema _$SchemaFromJson(Map<String, dynamic> json) {
discriminator:
json['discriminator'] == null ? null : Discriminator.fromJson(json['discriminator'] as Map<String, dynamic>),
pattern: json['pattern'] as String?,
minLength: json['minLength'] as int?,
maxLength: json['maxLength'] as int?,
);
}
@ -83,5 +87,7 @@ Map<String, dynamic> _$SchemaToJson(Schema instance) {
writeNotNull('contentSchema', instance.contentSchema?.toJson());
writeNotNull('discriminator', instance.discriminator?.toJson());
writeNotNull('pattern', instance.pattern);
writeNotNull('minLength', instance.minLength);
writeNotNull('maxLength', instance.maxLength);
return val;
}

26
packages/dynamite/lib/src/openapi_builder.dart

@ -582,12 +582,28 @@ class OpenAPIBuilder implements Builder {
parameter.schema!,
);
if (result.name == 'String' && parameter.schema?.pattern != null) {
code.write('''
if (!RegExp(r'${parameter.schema!.pattern!}').hasMatch(${_toDartName(parameter.name)})) {
throw Exception('Invalid value "\$${_toDartName(parameter.name)}" for parameter "${_toDartName(parameter.name)}" with pattern "' r'${parameter.schema!.pattern!}"'); // coverage:ignore-line
if (result.name == 'String') {
if (parameter.schema?.pattern != null) {
code.write('''
if (!RegExp(r'${parameter.schema!.pattern!}').hasMatch(${_toDartName(parameter.name)})) {
throw Exception('Invalid value "\$${_toDartName(parameter.name)}" for parameter "${_toDartName(parameter.name)}" with pattern "' r'${parameter.schema!.pattern!}"'); // coverage:ignore-line
}
''');
}
if (parameter.schema?.minLength != null) {
code.write('''
if (${_toDartName(parameter.name)}.length < ${parameter.schema!.minLength!}) {
throw Exception('Parameter "${_toDartName(parameter.name)}" has to be at least ${parameter.schema!.minLength!} characters long'); // coverage:ignore-line
}
''');
}
if (parameter.schema?.maxLength != null) {
code.write('''
if (${_toDartName(parameter.name)}.length > ${parameter.schema!.maxLength!}) {
throw Exception('Parameter "${_toDartName(parameter.name)}" has to be at most ${parameter.schema!.maxLength!} characters long'); // coverage:ignore-line
}
''');
}
''');
}
final defaultValueCode = parameter.schema?.default_ != null

Loading…
Cancel
Save