File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/resources/js/global/models/validationResponse.js
/**
* ValidationResponse.
*
* Represents a response that originates from
*/
class ValidationResponse {
constructor()
{
this._valid = false;
this._errors = [];
this.toJSON = this._toJson.bind(this);
}
/**
* @param {string} json
* @return {ValidationResponse|null}
*/
static fromJsonString(json) {
if(!this.is(json)) {
return null;
}
let jsonObject = JSON.parse(json);
let instance = new this;
instance._valid = jsonObject.valid;
instance._errors = jsonObject.errors;
return instance;
}
/**
* Checks that the given json string represents a ErrorResponse
*
* @param {string} json
* @param {boolean} logErrors
* @return {boolean}
*/
static is(json, logErrors = true)
{
let jsonObject = null;
try {
jsonObject = JSON.parse(json);
if(!jsonObject) return false;
} catch (e) {
console.error('ValidationResponse: The given json does not represent a ValidationResponse since the json string was not a valid json. Object: ', jsonObject);
return false;
}
if(!jsonObject.hasOwnProperty('valid')) {
console.error('ValidationResponse: The given json object must have an "valid" property that is a string. But did not have one. Object: ', jsonObject);
return false;
}
if(!jsonObject.hasOwnProperty('errors') || typeof !jsonObject.errors == 'object') {
console.error('ValidationResponse: The given json object must have an errors object. But did not have one. Object: ', jsonObject);
return false;
}
return true;
}
/**
* @return {{valid: boolean, errors: Array}}
* @private
*/
_toJson() {
return {
'valid': this._valid,
'errors': this._errors
}
}
/**
* @return {boolean}
*/
get valid() {
return this._valid;
}
/**
* @return {Array}
*/
get errors() {
return this._errors;
}
}
export { ValidationResponse }