Check for required fields
This commit is contained in:
parent
46d4c24f69
commit
756a154551
3 changed files with 17 additions and 0 deletions
|
@ -4,6 +4,7 @@ import 'package:analyzer/dart/element/type.dart';
|
|||
import 'package:analyzer/src/dart/element/element.dart';
|
||||
import 'package:angel_serialize/angel_serialize.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:recase/recase.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
|
@ -72,6 +73,18 @@ Future<BuildContext> buildContext(
|
|||
ctx.aliases[field.name] = new ReCase(field.name).snakeCase;
|
||||
}
|
||||
|
||||
// Check for @required
|
||||
var required =
|
||||
const TypeChecker.fromRuntime(Required).firstAnnotationOf(el);
|
||||
|
||||
if (required != null) {
|
||||
var cr = new ConstantReader(required);
|
||||
var reason = cr.peek('reason')?.stringValue ??
|
||||
"Missing field '${ctx.resolveFieldName(field.name)}' on ${ctx
|
||||
.modelClassName}.";
|
||||
ctx.requiredFields[field.name] = reason;
|
||||
}
|
||||
|
||||
ctx.fields.add(field);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,6 +9,9 @@ class BuildContext {
|
|||
ReCase _modelClassNameRecase;
|
||||
TypeReference _modelClassType;
|
||||
|
||||
/// A map of fields that are absolutely required, and error messages for when they are absent.
|
||||
final Map<String, String> requiredFields = {};
|
||||
|
||||
/// A map of field names to resolved names from `@Alias()` declarations.
|
||||
final Map<String, String> aliases = {};
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ dependencies:
|
|||
code_buffer: ^1.0.0
|
||||
code_builder: ^3.0.0
|
||||
id: ^1.0.0
|
||||
meta: ^1.0.0
|
||||
recase: ^1.0.0
|
||||
source_gen: ^0.7.0
|
||||
dev_dependencies:
|
||||
|
|
Loading…
Reference in a new issue