Skip to content

Commit 947d453

Browse files
committed
fix(ExampleGenerator): example should be one of the options if type is enum
1 parent c23c10a commit 947d453

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/ExampleGenerator.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,14 @@ declare class ExampleGenerator extends AmfHelperMixin(Object) {
333333
*/
334334
_computeDefaultRangeValue(range: object): string | number | boolean | null;
335335

336+
/**
337+
* Computes default enum value for given range.
338+
*
339+
* @param {Object} range AMF's range definition for a shape.
340+
* @return {string|number|boolean|null} Value cast to the corresponding type
341+
*/
342+
_computeDefaultEnumRangeValue(range: object): string | number | boolean | null;
343+
336344
/**
337345
* Casts the value to given data type represented in AMF notation.
338346
*

src/ExampleGenerator.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,31 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
13261326
return this._typeToValue(value, dt['@id']);
13271327
}
13281328

1329+
/**
1330+
* Computes default enum value for given range.
1331+
*
1332+
* @param {Object} range AMF's range definition for a shape.
1333+
* @return {string|number|boolean|null} Value cast to the corresponding type
1334+
*/
1335+
_computeDefaultEnumRangeValue(range) {
1336+
let enumOptions = range[this._getAmfKey(this.ns.w3.shacl.in)]
1337+
if (Array.isArray(enumOptions)) {
1338+
[enumOptions] = enumOptions;
1339+
}
1340+
const rdfKey = this._getAmfKey(this.ns.w3.rdfSchema.key);
1341+
const firstOptionKey = Object.keys(enumOptions).find((key) => key.indexOf(rdfKey) !== -1);
1342+
let firstOption = enumOptions[firstOptionKey];
1343+
if (Array.isArray(firstOption)) {
1344+
[firstOption] = firstOption;
1345+
}
1346+
const vKey = this._getAmfKey(this.ns.raml.vocabularies.data.value);
1347+
const value = this._getValue(firstOption, vKey);
1348+
if (value) {
1349+
return value;
1350+
}
1351+
return null;
1352+
}
1353+
13291354
/**
13301355
* Computes default value for given range.
13311356
*
@@ -1338,6 +1363,14 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
13381363
* @return {string|number|boolean|null} Value casted to the corresponding type
13391364
*/
13401365
_computeDefaultRangeValue(range) {
1366+
const isEnum = this._hasProperty(range, this.ns.w3.shacl.in)
1367+
if (isEnum) {
1368+
const value = this._computeDefaultEnumRangeValue(range);
1369+
if (value) {
1370+
return value;
1371+
}
1372+
}
1373+
13411374
const type = this._computeScalarType(range);
13421375
switch (type) {
13431376
case 'Number':

0 commit comments

Comments
 (0)