[JS] Refactor oneOf validation logic, add more tests (#13575)

* refactor oneof validation logic, add more tests

* update samples
This commit is contained in:
William Cheng
2022-10-03 22:32:29 +08:00
committed by GitHub
parent 0581d81eaa
commit c39fc1e31b
8 changed files with 689 additions and 570 deletions

View File

@@ -23,10 +23,93 @@ class Color {
* Constructs a new <code>Color</code>.
* RGB array, RGBA array, or hex string.
* @alias module:model/Color
* @param {(module:model/String|module:model/[Number])} The actual instance to initialize Color.
* @param {(module:model/String|module:model/[Number])} instance The actual instance to initialize Color.
*/
constructor(obj = null) {
this.actualInstance = obj;
constructor(instance = null) {
if (instance === null) {
this.actualInstance = null;
return;
}
var match = 0;
var errorMessages = [];
// RGB three element array with values 0-255.
try {
// validate array data type
if (!Array.isArray(instance)) {
throw new Error("Invalid data type. Expecting array. Input: " + instance);
}
if (instance.length > 3 || instance.length < 3) {
throw new Error("Invalid array size. Minimim: 3. Maximum: 3. Input: " + instance);
}
// validate array of integer
for (const item of instance) {
if (!(typeof item === 'number' && item % 1 === 0)) {
throw new Error("Invalid array items. Must be integer. Input: " + instance);
}
if (item > 255 || item < 0) {
throw new Error("Invalid integer value in an array items. Max.: 255. Min.: 0. Input: " + instance);
}
}
this.actualInstance = instance;
match++;
} catch(err) {
// json data failed to deserialize into [Number]
errorMessages.push("Failed to construct [Number]: " + err)
}
// RGBA four element array with values 0-255.
try {
// validate array data type
if (!Array.isArray(instance)) {
throw new Error("Invalid data type. Expecting array. Input: " + instance);
}
if (instance.length > 4 || instance.length < 4) {
throw new Error("Invalid array size. Minimim: 4. Maximum: 4. Input: " + instance);
}
// validate array of integer
for (const item of instance) {
if (!(typeof item === 'number' && item % 1 === 0)) {
throw new Error("Invalid array items. Must be integer. Input: " + instance);
}
if (item > 255 || item < 0) {
throw new Error("Invalid integer value in an array items. Max.: 255. Min.: 0. Input: " + instance);
}
}
this.actualInstance = instance;
match++;
} catch(err) {
// json data failed to deserialize into [Number]
errorMessages.push("Failed to construct [Number]: " + err)
}
// Hex color string, such as #00FF00.
try {
// validate array of string
if (!(typeof instance === 'string')) {
throw new Error("Invalid input. Must be string. Input: " + JSON.stringify(instance));
}
if (!/^#(?:[0-9a-fA-F]{3}){1,2}$/.test(instance)) {
throw new Error("Invalid string value in an array items. Must conform to /^#(?:[0-9a-fA-F]{3}){1,2}$/. Input: " + JSON.stringify(instance));
}
if (instance.length > 7 && instance.length < 7) {
throw new Error("Invalid string value in an array items. Max. length: 7. Min. length: 7. Input: " + JSON.stringify(instance));
}
this.actualInstance = instance;
match++;
} catch(err) {
// json data failed to deserialize into String
errorMessages.push("Failed to construct String: " + err)
}
if (match > 1) {
throw new Error("Multiple matches found constructing `Color` with oneOf schemas String, [Number]. Input: " + JSON.stringify(instance));
} else if (match === 0) {
this.actualInstance = null; // clear the actual instance in case there are multiple matches
throw new Error("No match found constructing `Color` with oneOf schemas String, [Number]. Details: " +
errorMessages.join(", "));
} else { // only 1 match
// the input is valid
}
}
/**
@@ -37,88 +120,7 @@ class Color {
* @return {module:model/Color} The populated <code>Color</code> instance.
*/
static constructFromObject(data, obj) {
if (!data) {
return new Color();
}
var match = 0;
var errorMessages = [];
// RGB three element array with values 0-255.
try {
// validate array data type
if (!Array.isArray(data)) {
throw new Error("Invalid data type. Expecting array. Data: " + data);
}
if (data.length > 3 || data.length < 3) {
throw new Error("Invalid array size. Minimim: 3. Maximum: 3. Data: " + data);
}
// validate array of integer
for (const item of data) {
if (!(typeof item === 'number' && item % 1 === 0)) {
throw new Error("Invalid array items. Must be integer. Data: " + data);
}
if (item > 255 || item < 0) {
throw new Error("Invalid integer value in an array items. Max.: 255. Min.: 0. Data: " + data);
}
}
obj = new Color(data);
match++;
} catch(err) {
// json data failed to deserialize into [Number]
errorMessages.push("Failed to construct [Number]: " + err)
}
// RGBA four element array with values 0-255.
try {
// validate array data type
if (!Array.isArray(data)) {
throw new Error("Invalid data type. Expecting array. Data: " + data);
}
if (data.length > 4 || data.length < 4) {
throw new Error("Invalid array size. Minimim: 4. Maximum: 4. Data: " + data);
}
// validate array of integer
for (const item of data) {
if (!(typeof item === 'number' && item % 1 === 0)) {
throw new Error("Invalid array items. Must be integer. Data: " + data);
}
if (item > 255 || item < 0) {
throw new Error("Invalid integer value in an array items. Max.: 255. Min.: 0. Data: " + data);
}
}
obj = new Color(data);
match++;
} catch(err) {
// json data failed to deserialize into [Number]
errorMessages.push("Failed to construct [Number]: " + err)
}
// Hex color string, such as #00FF00.
try {
// validate array of string
if (!(typeof data === 'string')) {
throw new Error("Invalid data. Must be string. Data: " + JSON.stringify(data));
}
if (!/^#(?:[0-9a-fA-F]{3}){1,2}$/.test(data)) {
throw new Error("Invalid string value in an array items. Must conform to /^#(?:[0-9a-fA-F]{3}){1,2}$/. Data: " + JSON.stringify(data));
}
if (data.length > 7 && data.length < 7) {
throw new Error("Invalid string value in an array items. Max. length: 7. Min. length: 7. Data: " + JSON.stringify(data));
}
obj = new Color(data);
match++;
} catch(err) {
// json data failed to deserialize into String
errorMessages.push("Failed to construct String: " + err)
}
if (match > 1) {
throw new Error("Multiple matches found constructing `Color` with oneOf schemas String, [Number]. JSON data: " + JSON.stringify(data));
} else if (match === 0) {
throw new Error("No match found constructing `Color` with oneOf schemas String, [Number]. Details: " +
errorMessages.join(", "));
} else { // only 1 match
return obj;
}
return new Color(data);
}
/**

View File

@@ -24,10 +24,56 @@ class Pig {
/**
* Constructs a new <code>Pig</code>.
* @alias module:model/Pig
* @param {(module:model/BasquePig|module:model/DanishPig)} The actual instance to initialize Pig.
* @param {(module:model/BasquePig|module:model/DanishPig)} instance The actual instance to initialize Pig.
*/
constructor(obj = null) {
this.actualInstance = obj;
constructor(instance = null) {
if (instance === null) {
this.actualInstance = null;
return;
}
var match = 0;
var errorMessages = [];
try {
if (typeof instance === "BasquePig") {
this.actualInstance = instance;
} else {
// plain JS object
// validate the object
BasquePig.validateJSON(instance); // throw an exception if no match
// create BasquePig from JS object
this.actualInstance = BasquePig.constructFromObject(instance);
}
match++;
} catch(err) {
// json data failed to deserialize into BasquePig
errorMessages.push("Failed to construct BasquePig: " + err)
}
try {
if (typeof instance === "DanishPig") {
this.actualInstance = instance;
} else {
// plain JS object
// validate the object
DanishPig.validateJSON(instance); // throw an exception if no match
// create DanishPig from JS object
this.actualInstance = DanishPig.constructFromObject(instance);
}
match++;
} catch(err) {
// json data failed to deserialize into DanishPig
errorMessages.push("Failed to construct DanishPig: " + err)
}
if (match > 1) {
throw new Error("Multiple matches found constructing `Pig` with oneOf schemas BasquePig, DanishPig. Input: " + JSON.stringify(instance));
} else if (match === 0) {
this.actualInstance = null; // clear the actual instance in case there are multiple matches
throw new Error("No match found constructing `Pig` with oneOf schemas BasquePig, DanishPig. Details: " +
errorMessages.join(", "));
} else { // only 1 match
// the input is valid
}
}
/**
@@ -38,41 +84,7 @@ class Pig {
* @return {module:model/Pig} The populated <code>Pig</code> instance.
*/
static constructFromObject(data, obj) {
if (!data) {
return new Pig();
}
var match = 0;
var errorMessages = [];
try {
// validate the JSON data
BasquePig.validateJSON(data);
// create BasquePig from JSON data
obj = new Pig(BasquePig.constructFromObject(data));
match++;
} catch(err) {
// json data failed to deserialize into BasquePig
errorMessages.push("Failed to construct BasquePig: " + err)
}
try {
// validate the JSON data
DanishPig.validateJSON(data);
// create DanishPig from JSON data
obj = new Pig(DanishPig.constructFromObject(data));
match++;
} catch(err) {
// json data failed to deserialize into DanishPig
errorMessages.push("Failed to construct DanishPig: " + err)
}
if (match > 1) {
throw new Error("Multiple matches found constructing `Pig` with oneOf schemas BasquePig, DanishPig. JSON data: " + JSON.stringify(data));
} else if (match === 0) {
throw new Error("No match found constructing `Pig` with oneOf schemas BasquePig, DanishPig. Details: " +
errorMessages.join(", "));
} else { // only 1 match
return obj;
}
return new Pig(data);
}
/**