[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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 689 additions and 570 deletions

View File

@ -11,11 +11,206 @@ class {{classname}} {
* Constructs a new <code>{{classname}}</code>.{{#description}}
* {{.}}{{/description}}
* @alias module:{{#invokerPackage}}{{.}}/{{/invokerPackage}}{{#modelPackage}}{{.}}/{{/modelPackage}}{{classname}}
* @param {{=< >=}}{(<#oneOf>module:<#invokerPackage><invokerPackage>/</invokerPackage><#modelPackage><modelPackage>/</modelPackage><&.><^-last>|</-last></oneOf>)}<={{ }}=> The actual instance to initialize {{classname}}.
* @param {{=< >=}}{(<#oneOf>module:<#invokerPackage><invokerPackage>/</invokerPackage><#modelPackage><modelPackage>/</modelPackage><&.><^-last>|</-last></oneOf>)}<={{ }}=> instance The actual instance to initialize {{classname}}.
*/
{{/emitJSDoc}}
constructor(obj = null) {
this.actualInstance = obj;
constructor(instance = null) {
if (instance === null) {
this.actualInstance = null;
return;
}
var match = 0;
var errorMessages = [];
{{#composedSchemas.oneOf}}
{{#description}}
// {{{description}}}
{{/description}}
try {
{{#isPrimitiveType}}
{{#isArray}}
// validate array data type
if (!Array.isArray(instance)) {
throw new Error("Invalid data type. Expecting array. Input: " + instance);
}
{{#minItems}}
{{#maxItems}}
if (instance.length > {{{maxItems}}} || instance.length < {{{minItems}}}) {
throw new Error("Invalid array size. Minimim: {{minItems}}. Maximum: {{maxItems}}. Input: " + instance);
}
{{/maxItems}}
{{^maxItems}}
if (instance.length < {{{minItems}}}) {
throw new Error("Invalid array size. Minimim: {{minItems}}. Input: " + instance);
}
{{/maxItems}}
{{/minItems}}
{{^minItems}}
{{#maxItems}}
if (instance.length > {{{maxItems}}}) {
throw new Error("Invalid array size. Maximum: {{maxItems}}. Input: " + instance);
}
{{/maxItems}}
{{/minItems}}
{{#items.isInteger}}
// 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);
}
{{#items.maximum}}
{{#items.minimum}}
if (item > {{items.maximum}} || item < {{items.minimum}}) {
throw new Error("Invalid integer value in an array items. Max.: {{items.maximum}}. Min.: {{items.minimum}}. Input: " + instance);
}
{{/items.minimum}}
{{^items.minimum}}
if (item > {{items.maximum}}) {
throw new Error("Invalid integer value in an array items. Max.: {{items.maximum}}. Input: " + instance);
}
{{/items.minimum}}
{{/items.maximum}}
{{^items.maximum}}
{{#items.minimum}}
if (item < {{items.minimum}}) {
throw new Error("Invalid integer value in an array items. Min.: {{items.minimum}}. Input: " + instance);
}
{{/items.minimum}}
{{/items.maximum}}
}
{{/items.isInteger}}
{{#items.isString}}
// validate array of string
for (const item of instance) {
if (!(typeof item === 'number' && item % 1 === 0)) {
throw new Error("Invalid array items. Must be string. Input: " + instance);
}
{{#items.pattern}}
if (!{{{items.pattern}}}.test(item)) {
throw new Error("Invalid string value in an array items. Must conform to {{{items.pattern}}}. Input: " + item);
}
{{/items.pattern}}
{{#items.minLength}}
{{#items.maxLength}}
if (item.length > {{items.maxLength}} && item.length < {{items.minLength}}) {
throw new Error("Invalid string value in an array items. Max. length: {{{items.maxLength}}}. Min. length: {{{items.minLength}}}. Input: " + item);
}
{{/items.maxLength}}
{{^items.maxLength}}
if (item.length < {{items.minLength}}) {
throw new Error("Invalid string value in an array items. Min. length: {{{items.minLength}}}. Input: " + item);
}
{{/items.maxLength}}
{{/items.minLength}}
{{^items.minLength}}
{{#items.maxLength}}
if (item.length > {{items.maxLength}}) {
throw new Error("Invalid string value in an array items. Max. length: {{{items.maxLength}}}. Input: " + item)
}
{{/items.maxLength}}
{{/items.minLength}}
}
{{/items.isString}}
{{#items.isNumber}}
// validate array of string
for (const item of instance) {
if (!(typeof instance === 'number' && instance % 1 != 0)) {
throw new Error("Invalid array items. Must be number. Input: " + JSON.stringify(instance));
}
}
{{/items.isNumber}}
{{/isArray}}
{{^isArray}}
{{#isInteger}}
// validate array of integer
if (!(typeof instance === 'number' && instance % 1 === 0)) {
throw new Error("Invalid array items. Must be integer. Input: " + JSON.stringify(instance));
}
{{#maximum}}
{{#minimum}}
if (instance > {{maximum}} || instance < {{minimum}}) {
throw new Error("Invalid integer value in an array items. Max.: {{maximum}}. Min.: {{minimum}}. Input: " + JSON.stringify(instance));
}
{{/minimum}}
{{^minimum}}
if (instance > {{maximum}}) {
throw new Error("Invalid integer value in an array items. Max.: {{maximum}}. Input: " + JSON.stringify(instance));
}
{{/minimum}}
{{/maximum}}
{{^maximum}}
{{#minimum}}
if (instance < {{minimum}}) {
throw new Error("Invalid integer value in an array items. Min.: {{minimum}}. Input: " + JSON.stringify(instance));
}
{{/minimum}}
{{/maximum}}
{{/isInteger}}
{{#isString}}
// validate array of string
if (!(typeof instance === 'string')) {
throw new Error("Invalid input. Must be string. Input: " + JSON.stringify(instance));
}
{{#pattern}}
if (!{{{pattern}}}.test(instance)) {
throw new Error("Invalid string value in an array items. Must conform to {{{.}}}. Input: " + JSON.stringify(instance));
}
{{/pattern}}
{{#minLength}}
{{#maxLength}}
if (instance.length > {{maxLength}} && instance.length < {{minLength}}) {
throw new Error("Invalid string value in an array items. Max. length: {{{maxLength}}}. Min. length: {{{minLength}}}. Input: " + JSON.stringify(instance));
}
{{/maxLength}}
{{^maxLength}}
if (instance.length < {{minLength}}) {
throw new Error("Invalid string value in an array items. Min. length: {{{minLength}}}. Input: " + instance);
}
{{/maxLength}}
{{/minLength}}
{{^minLength}}
{{#maxLength}}
if (instance.length > {{maxLength}}) {
throw new Error("Invalid string value in an array items. Max. length: {{{maxLength}}}. Input: " + JSON.stringify(instance));
}
{{/maxLength}}
{{/minLength}}
{{/isString}}
{{#isNumber}}
// validate array of string
if (!(typeof instance === 'number' && instance % 1 != 0)) {
throw new Error("Invalid array items. Must be number. Input: " + JSON.stringify(instance));
}
{{/isNumber}}
{{/isArray}}
this.actualInstance = instance;
{{/isPrimitiveType}}
{{^isPrimitiveType}}
if (typeof instance === "{{{dataType}}}") {
this.actualInstance = instance;
} else {
// plain JS object
// validate the object
{{{dataType}}}.validateJSON(instance); // throw an exception if no match
// create {{{dataType}}} from JS object
this.actualInstance = {{{dataType}}}.constructFromObject(instance);
}
{{/isPrimitiveType}}
match++;
} catch(err) {
// json data failed to deserialize into {{{dataType}}}
errorMessages.push("Failed to construct {{{dataType}}}: " + err)
}
{{/composedSchemas.oneOf}}
if (match > 1) {
throw new Error("Multiple matches found constructing `{{{classname}}}` with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. 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 `{{{classname}}}` with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Details: " +
errorMessages.join(", "));
} else { // only 1 match
// the input is valid
}
}
{{#emitJSDoc}}
@ -28,195 +223,7 @@ class {{classname}} {
*/
{{/emitJSDoc}}
static constructFromObject(data, obj) {
if (!data) {
return new {{classname}}();
}
var match = 0;
var errorMessages = [];
{{#composedSchemas.oneOf}}
{{#description}}
// {{{description}}}
{{/description}}
try {
{{#isPrimitiveType}}
{{#isArray}}
// validate array data type
if (!Array.isArray(data)) {
throw new Error("Invalid data type. Expecting array. Data: " + data);
}
{{#minItems}}
{{#maxItems}}
if (data.length > {{{maxItems}}} || data.length < {{{minItems}}}) {
throw new Error("Invalid array size. Minimim: {{minItems}}. Maximum: {{maxItems}}. Data: " + data);
}
{{/maxItems}}
{{^maxItems}}
if (data.length < {{{minItems}}}) {
throw new Error("Invalid array size. Minimim: {{minItems}}. Data: " + data);
}
{{/maxItems}}
{{/minItems}}
{{^minItems}}
{{#maxItems}}
if (data.length > {{{maxItems}}}) {
throw new Error("Invalid array size. Maximum: {{maxItems}}. Data: " + data);
}
{{/maxItems}}
{{/minItems}}
{{#items.isInteger}}
// 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);
}
{{#items.maximum}}
{{#items.minimum}}
if (item > {{items.maximum}} || item < {{items.minimum}}) {
throw new Error("Invalid integer value in an array items. Max.: {{items.maximum}}. Min.: {{items.minimum}}. Data: " + data);
}
{{/items.minimum}}
{{^items.minimum}}
if (item > {{items.maximum}}) {
throw new Error("Invalid integer value in an array items. Max.: {{items.maximum}}. Data: " + data);
}
{{/items.minimum}}
{{/items.maximum}}
{{^items.maximum}}
{{#items.minimum}}
if (item < {{items.minimum}}) {
throw new Error("Invalid integer value in an array items. Min.: {{items.minimum}}. Data: " + data);
}
{{/items.minimum}}
{{/items.maximum}}
}
{{/items.isInteger}}
{{#items.isString}}
// validate array of string
for (const item of data) {
if (!(typeof item === 'number' && item % 1 === 0)) {
throw new Error("Invalid array items. Must be string. Data: " + data);
}
{{#items.pattern}}
if (!{{{items.pattern}}}.test(item)) {
throw new Error("Invalid string value in an array items. Must conform to {{{items.pattern}}}. Data: " + item);
}
{{/items.pattern}}
{{#items.minLength}}
{{#items.maxLength}}
if (item.length > {{items.maxLength}} && item.length < {{items.minLength}}) {
throw new Error("Invalid string value in an array items. Max. length: {{{items.maxLength}}}. Min. length: {{{items.minLength}}}. Data: " + item);
}
{{/items.maxLength}}
{{^items.maxLength}}
if (item.length < {{items.minLength}}) {
throw new Error("Invalid string value in an array items. Min. length: {{{items.minLength}}}. Data: " + item);
}
{{/items.maxLength}}
{{/items.minLength}}
{{^items.minLength}}
{{#items.maxLength}}
if (item.length > {{items.maxLength}}) {
throw new Error("Invalid string value in an array items. Max. length: {{{items.maxLength}}}. Data: " + item)
}
{{/items.maxLength}}
{{/items.minLength}}
}
{{/items.isString}}
{{#items.isNumber}}
// validate array of string
for (const item of data) {
if (!(typeof data === 'number' && data % 1 != 0)) {
throw new Error("Invalid array items. Must be number. Data: " + JSON.stringify(data));
}
}
{{/items.isNumber}}
{{/isArray}}
{{^isArray}}
{{#isInteger}}
// validate array of integer
if (!(typeof data === 'number' && data % 1 === 0)) {
throw new Error("Invalid array items. Must be integer. Data: " + JSON.stringify(data));
}
{{#maximum}}
{{#minimum}}
if (data > {{maximum}} || data < {{minimum}}) {
throw new Error("Invalid integer value in an array items. Max.: {{maximum}}. Min.: {{minimum}}. Data: " + JSON.stringify(data));
}
{{/minimum}}
{{^minimum}}
if (data > {{maximum}}) {
throw new Error("Invalid integer value in an array items. Max.: {{maximum}}. Data: " + JSON.stringify(data));
}
{{/minimum}}
{{/maximum}}
{{^maximum}}
{{#minimum}}
if (data < {{minimum}}) {
throw new Error("Invalid integer value in an array items. Min.: {{minimum}}. Data: " + JSON.stringify(data));
}
{{/minimum}}
{{/maximum}}
{{/isInteger}}
{{#isString}}
// validate array of string
if (!(typeof data === 'string')) {
throw new Error("Invalid data. Must be string. Data: " + JSON.stringify(data));
}
{{#pattern}}
if (!{{{pattern}}}.test(data)) {
throw new Error("Invalid string value in an array items. Must conform to {{{.}}}. Data: " + JSON.stringify(data));
}
{{/pattern}}
{{#minLength}}
{{#maxLength}}
if (data.length > {{maxLength}} && data.length < {{minLength}}) {
throw new Error("Invalid string value in an array items. Max. length: {{{maxLength}}}. Min. length: {{{minLength}}}. Data: " + JSON.stringify(data));
}
{{/maxLength}}
{{^maxLength}}
if (data.length < {{minLength}}) {
throw new Error("Invalid string value in an array items. Min. length: {{{minLength}}}. Data: " + data);
}
{{/maxLength}}
{{/minLength}}
{{^minLength}}
{{#maxLength}}
if (data.length > {{maxLength}}) {
throw new Error("Invalid string value in an array items. Max. length: {{{maxLength}}}. Data: " + JSON.stringify(data));
}
{{/maxLength}}
{{/minLength}}
{{/isString}}
{{#isNumber}}
// validate array of string
if (!(typeof data === 'number' && data % 1 != 0)) {
throw new Error("Invalid array items. Must be number. Data: " + JSON.stringify(data));
}
{{/isNumber}}
{{/isArray}}
obj = new {{classname}}(data);
{{/isPrimitiveType}}
{{^isPrimitiveType}}
// validate the JSON data
{{{dataType}}}.validateJSON(data);
// create {{{dataType}}} from JSON data
obj = new {{classname}}({{{dataType}}}.constructFromObject(data));
{{/isPrimitiveType}}
match++;
} catch(err) {
// json data failed to deserialize into {{{dataType}}}
errorMessages.push("Failed to construct {{{dataType}}}: " + err)
}
{{/composedSchemas.oneOf}}
if (match > 1) {
throw new Error("Multiple matches found constructing `{{{classname}}}` with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. JSON data: " + JSON.stringify(data));
} else if (match === 0) {
throw new Error("No match found constructing `{{{classname}}}` with oneOf schemas {{#oneOf}}{{{.}}}{{^-last}}, {{/-last}}{{/oneOf}}. Details: " +
errorMessages.join(", "));
} else { // only 1 match
return obj;
}
return new {{classname}}(data);
}
{{#emitJSDoc}}

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);
}
/**

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);
}
/**

View File

@ -74,6 +74,12 @@ describe('Petstore', function() {
}
});
it('should run Pig new correctly', function() {
var bpig = OpenAPIPetstore.BasquePig.constructFromObject(JSON.parse('{"className":"BasquePig","color":"red"}'));
var pig = new OpenAPIPetstore.Pig(bpig);
expect(JSON.stringify(pig)).to.be('{"className":"BasquePig","color":"red"}');
});
it('should run Pig constructFromObject correctly', function() {
var bpig = '{"className":"BasquePig","color":"red"}';
var pig = OpenAPIPetstore.Pig.constructFromObject(JSON.parse(bpig));
@ -96,14 +102,6 @@ describe('Petstore', function() {
//expect(tag_result.id).to.be(1);
//expect(JSON.stringify(tag_result)).to.be(tag_json);
});
it('should deserialize nested oneOf models correctly', function() {
var nested_one_of_json = '{"size":28,"nested_pig":{"className":"BasquePig","color":"red"}}'
var result = OpenAPIPetstore.ApiClient.convertToType(JSON.parse(nested_one_of_json), OpenAPIPetstore.NestedOneOf);
expect(result).to.be.a(OpenAPIPetstore.NestedOneOf);
expect(JSON.stringify(result)).to.be(nested_one_of_json);
});
it('should run Color constructFromObject correctly from array', function() {
// construct from RgbColor
let array_integer = [0,128,255];
@ -123,6 +121,23 @@ describe('Petstore', function() {
}
});
it('should run Color new correctly', function() {
// valid hex color
var input = "#00FF00";
var color = new OpenAPIPetstore.Color(input);
expect(color.getActualInstance()).to.be(input);
// valid RgbColor
input = [0,128,255];
color = new OpenAPIPetstore.Color(input);
expect(color.getActualInstance()).to.be(input);
// valid RgbaColor
input = [0,128,200,255];
color = new OpenAPIPetstore.Color(input);
expect(color.getActualInstance()).to.be(input);
});
it('should run Color constructFromObject correctly', function() {
// valid hex color
var json = '"#00FF00"';
@ -140,6 +155,54 @@ describe('Petstore', function() {
expect(JSON.stringify(color)).to.be(json);
});
it('should thrown an error when running Color new with invalid data', function() {
// invalid hex color
try {
let input = "#00FF00ZZZZ";
new OpenAPIPetstore.Color(input);
expect(true).to.be(false); // this line should not run if the error is thrown correctly
} catch (err) {
expect(err).to.be.eql(new Error('No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid data type. Expecting array. Data: #00FF00ZZZZ, Failed to desserialize JSON data into [Number]: Error: Invalid data type. Expecting array. Data: #00FF00ZZZZ, Failed to desserialize JSON data into String: Error: Invalid string value in an array items. Must conform to /^#(?:[0-9a-fA-F]{3}){1,2}$/. Data: "#00FF00ZZZZ"'));
}
// invalid RgbColor <0
try {
let input = [-1,128,255];
new OpenAPIPetstore.Color(input);
expect(true).to.be(false); // this line should not run if the error is thrown correctly
} catch (err) {
expect(err).to.be.eql(new Error('No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: -1,128,255, Failed to desserialize JSON data into [Number]: Error: Invalid array size. Minimim: 4. Maximum: 4. Data: -1,128,255, Failed to desserialize JSON data into String: Error: Invalid data. Must be string. Data: [-1,128,255]'));
}
// invalid RgbColor >255
try {
let input = [1,128,256];
new OpenAPIPetstore.Color(input);
expect(true).to.be(false); // this line should not run if the error is thrown correctly
} catch (err) {
expect(err).to.be.eql(new Error('No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: 1,128,256, Failed to desserialize JSON data into [Number]: Error: Invalid array size. Minimim: 4. Maximum: 4. Data: 1,128,256, Failed to desserialize JSON data into String: Error: Invalid data. Must be string. Data: [1,128,256]'));
}
// invalid RgbaColor <0
try {
let input = [-1,1,128,255];
new OpenAPIPetstore.Color(input);
expect(true).to.be(false); // this line should not run if the error is thrown correctly
} catch (err) {
expect(err).to.be.eql(new Error('No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid array size. Minimim: 3. Maximum: 3. Data: -1,1,128,255, Failed to desserialize JSON data into [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: -1,1,128,255, Failed to desserialize JSON data into String: Error: Invalid data. Must be string. Data: [-1,1,128,255]'));
}
// invalid RgbaColor >255
try {
let input = [1,11,128,256];
new OpenAPIPetstore.Color(input);
expect(true).to.be(false); // this line should not run if the error is thrown correctly
} catch (err) {
expect(err).to.be.eql(new Error('[Error: No match found constructing Color with oneOf schemas String, [Number]. Details: Failed to desserialize JSON data into [Number]: Error: Invalid array size. Minimim: 3. Maximum: 3. Data: 1,11,128,256, Failed to desserialize JSON data into [Number]: Error: Invalid integer value in an array items. Max.: 255. Min.: 0. Data: 1,11,128,256, Failed to desserialize JSON data into String: Error: Invalid data. Must be string. Data: [1,11,128,256]'));
}
});
it('should thrown an error when running Color constructFromObject with invalid data', function() {
// invalid hex color
try {
@ -203,13 +266,20 @@ describe('Petstore', function() {
expect(JSON.stringify(color)).to.be(json);
});
it('should deserialize nested oneOf models correctly', function() {
it('should deserialize NestedColor with nested oneOf model color correctly', function() {
var json = '{"nested":"#00FF00","size":256}'
var result = OpenAPIPetstore.ApiClient.convertToType(JSON.parse(json), OpenAPIPetstore.NestedColor);
expect(result).to.be.a(OpenAPIPetstore.NestedColor);
expect(JSON.stringify(result)).to.be('{"size":256,"nested":"#00FF00"}');
});
it('should deserialize NestedOneOf with nested oneOf model correctly', function() {
var nested_one_of_json = '{"size":28,"nested_pig":{"className":"BasquePig","color":"red"}}'
var result = OpenAPIPetstore.ApiClient.convertToType(JSON.parse(nested_one_of_json), OpenAPIPetstore.NestedOneOf);
expect(result).to.be.a(OpenAPIPetstore.NestedOneOf);
expect(JSON.stringify(result)).to.be(nested_one_of_json);
});
});
});

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);
}
/**