isValid method
isvalid method returns a bool of whether json record is valid to scheme or not and appends any deviations from scheme to ErrorFlags List
Implementation
bool isValid() {
bool retVal = true;
for (var key in this.scheme.keys) {
List<dynamic>? currentValue = this.scheme[key];
//checking existing keys, required keys and data types
//first element in scheme list of each item, represents data type
//second element represents if key is required or not required in scheme
if (this.targetJSON.keys.contains(key) &&
(this.targetJSON[key].runtimeType.toString() != currentValue?[0])) {
retVal = false;
this.errorFlags.add(
"${key} has a different data type of: ${this.targetJSON[key].runtimeType.toString()} while expected is ${currentValue?[0]}");
break;
} else if (!this.targetJSON.keys.contains(key) && currentValue?[1]) {
retVal = false;
this.errorFlags.add("${key} is required but not existing");
break;
}
//checking mandatory values, third element in scheme list of each item would be a list represent possible values of element.
if (retVal &&
(currentValue!.length > 2) &&
!currentValue[2].contains(this.targetJSON[key])) {
retVal = false;
this
.errorFlags
.add("${key} exists but have different value than the required");
break;
}
}
return retVal;
}