• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

juancastillo0 / json_form / 10909757877

17 Sep 2024 07:29PM UTC coverage: 78.271% (+3.9%) from 74.365%
10909757877

Pull #6

github

web-flow
Merge 549f44dbe into 419da37b9
Pull Request #6: Form value in controller recursive

380 of 453 new or added lines in 27 files covered. (83.89%)

22 existing lines in 5 files now uncovered.

1639 of 2094 relevant lines covered (78.27%)

1.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.96
/lib/src/models/object_schema.dart
1
import 'package:json_form/src/models/models.dart';
2

3
class SchemaObject extends Schema {
4
  SchemaObject({
1✔
5
    required super.id,
6
    required super.defs,
7
    required super.oneOf,
8
    this.required = const [],
9
    required this.dependentRequired,
10
    required this.dependentSchemas,
11
    super.title,
12
    super.description,
13
    required super.nullable,
14
    super.requiredProperty,
15
    super.parent,
16
    super.dependentsAddedBy,
17
  }) : super(type: SchemaType.object);
1✔
18

19
  factory SchemaObject.fromJson(
1✔
20
    String id,
21
    Map<String, Object?> json, {
22
    Schema? parent,
23
  }) {
24
    final dependentSchemas = <String, Schema>{};
1✔
25
    final dependentRequired = <String, List<String>>{};
1✔
26
    final schema = SchemaObject(
1✔
27
      id: id,
28
      title: json['title'] as String?,
1✔
29
      description: json['description'] as String?,
1✔
30
      required:
31
          json["required"] != null ? (json["required"]! as List).cast() : [],
4✔
32
      nullable: SchemaType.isNullable(json['type']),
2✔
33
      dependentRequired: dependentRequired,
34
      dependentSchemas: dependentSchemas,
35
      oneOf: json['oneOf'] as List?,
1✔
36
      defs: ((json['\$defs'] ?? json['definitions']) as Map?)?.cast(),
3✔
37
      parent: parent,
38
    );
39
    schema.dependentsAddedBy.addAll(parent?.dependentsAddedBy ?? const []);
3✔
40

41
    (json['dependencies'] as Map<String, dynamic>?)?.forEach((key, value) {
3✔
42
      if (value is List) {
1✔
43
        dependentRequired[key] = value.cast();
×
44
      } else {
45
        dependentSchemas[key] =
1✔
46
            Schema.fromJson(value as Map<String, dynamic>, parent: schema);
1✔
47
      }
48
    });
49
    (json['dependentSchemas'] as Map<String, dynamic>?)?.forEach((key, value) {
3✔
50
      dependentSchemas[key] =
1✔
51
          Schema.fromJson(value as Map<String, dynamic>, parent: schema);
1✔
52
    });
53
    (json['dependentRequired'] as Map<String, dynamic>?)?.forEach((key, value) {
3✔
54
      dependentRequired[key] = (value as List).cast();
2✔
55
    });
56

57
    final properties = json['properties'] as Map<String, dynamic>? ?? {};
2✔
58
    schema._setConstPropertiesDependencies(properties);
1✔
59
    schema._setProperties(properties);
1✔
60

61
    return schema;
62
  }
63

UNCOV
64
  @override
×
65
  Schema copyWith({
66
    required String id,
67
    Schema? parent,
68
    List<String>? dependentsAddedBy,
69
  }) {
UNCOV
70
    final newSchema = SchemaObject(
×
71
      id: id,
UNCOV
72
      defs: defs,
×
UNCOV
73
      title: title,
×
UNCOV
74
      description: description,
×
UNCOV
75
      required: required,
×
UNCOV
76
      nullable: nullable,
×
UNCOV
77
      parent: parent ?? this.parent,
×
UNCOV
78
      dependentsAddedBy: dependentsAddedBy ?? this.dependentsAddedBy,
×
UNCOV
79
      dependentSchemas: dependentSchemas,
×
UNCOV
80
      dependentRequired: dependentRequired,
×
UNCOV
81
      oneOf: oneOf,
×
82
    );
83

UNCOV
84
    newSchema.properties.addAll(
×
UNCOV
85
      properties.map(
×
UNCOV
86
        (e) => e.copyWith(
×
UNCOV
87
          id: e.id,
×
88
          parent: newSchema,
UNCOV
89
          dependentsAddedBy: newSchema.dependentsAddedBy,
×
90
        ),
91
      ),
92
    );
UNCOV
93
    newSchema.setUiSchema(uiSchema.toJson(), fromOptions: false);
×
94

95
    return newSchema;
96
  }
97

98
  /// array of required keys
99
  final List<String> required;
100
  final List<Schema> properties = [];
101

102
  /// the dependencies keyword from an earlier draft of JSON Schema
103
  /// (note that this is not part of the latest JSON Schema spec, though).
104
  /// Dependencies can be used to create dynamic schemas that change fields based on what data is entered
105
  final Map<String, Schema> dependentSchemas;
106
  final Map<String, List<String>> dependentRequired;
107

108
  @override
1✔
109
  void setUiSchema(
110
    Map<String, Object?> data, {
111
    required bool fromOptions,
112
  }) {
113
    super.setUiSchema(data, fromOptions: fromOptions);
1✔
114
    // set UI Schema to their properties
115
    for (final property_ in properties) {
2✔
116
      final v = data[property_.id] as Map<String, Object?>?;
2✔
117
      if (v != null || uiSchema.globalOptions != null) {
2✔
118
        property_.setUiSchema(v ?? {}, fromOptions: false);
2✔
119
        uiSchema.children[property_.id] = property_.uiSchema;
5✔
120
      }
121
    }
122

123
    // order logic
124
    final order = uiSchema.order;
2✔
125
    if (order != null) {
126
      properties.sort((a, b) {
3✔
127
        return order.indexOf(a.id) - order.indexOf(b.id);
5✔
128
      });
129
    }
130
  }
131

132
  void _setConstPropertiesDependencies(Map<String, dynamic> properties) {
1✔
133
    /// this is from dependentSchemas
134
    if (id.isEmpty && parent is SchemaObject) return;
4✔
135

136
    final constProperties = <String, List<SchemaProperty>>{};
1✔
137
    for (final o in oneOf) {
2✔
138
      if (o is SchemaObject) {
1✔
139
        for (final p in o.properties) {
2✔
140
          if (!dependentSchemas.containsKey(p.id) &&
3✔
141
              p is SchemaProperty &&
1✔
142
              p.constValue != null) {
1✔
143
            constProperties[p.id] = (constProperties[p.id] ?? [])..add(p);
6✔
144
          }
145
        }
146
      }
147
    }
148
    constProperties.forEach((key, value) {
2✔
149
      if (value.length != oneOf.length) return;
4✔
150
      final prop = properties[key];
1✔
151
      final propEnum = prop is Map ? prop['enum'] : null;
1✔
152
      final enumm = value.map((v) => v.constValue).toList();
4✔
153
      if (prop == null) {
154
        final enumNames = value
155
            .map(
1✔
156
              (v) => v.uiSchema.enumNames != null &&
3✔
157
                      v.uiSchema.enumNames!.isNotEmpty
×
158
                  ? v.uiSchema.enumNames!.first
×
159
                  : v.title,
1✔
160
            )
161
            .toList();
1✔
162
        properties[key] = <String, dynamic>{
2✔
163
          'enum': enumm,
1✔
164
          if (enumNames.every((n) => n != null && n.isNotEmpty))
2✔
165
            'ui:enumNames': enumNames,
×
166
        };
167
      } else if (propEnum is! List ||
×
168
          propEnum.length != enumm.length ||
×
169
          !propEnum.every(enumm.contains)) {
×
170
        return;
171
      }
172
      dependentSchemas[key] = SchemaObject(
3✔
173
        id: key,
174
        defs: {},
1✔
175
        oneOf: oneOf,
1✔
176
        required: [],
1✔
177
        dependentSchemas: {},
1✔
178
        dependentRequired: {},
1✔
179
        nullable: false,
180
        parent: this,
181
      );
182
    });
183
  }
184

185
  void _setProperties(Map<String, dynamic> properties) {
1✔
186
    properties.forEach((key, _property) {
2✔
187
      final isRequired = required.contains(key);
2✔
188

189
      final property = Schema.fromJson(
1✔
190
        _property as Map<String, dynamic>,
191
        id: key,
192
        parent: this,
193
      );
194

195
      property.requiredProperty = isRequired;
1✔
196
      if (property is SchemaProperty) {
1✔
197
        // Assign the properties that depend on this one
198
        property.setDependents(this);
1✔
199
      }
200

201
      this.properties.add(property);
2✔
202
    });
203
  }
204
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc