diff --git a/lib/src/parsers/__spec-examples__/schemaprops.ts b/lib/src/parsers/__spec-examples__/schemaprops.ts index 3f4a0cf13..3d4520501 100644 --- a/lib/src/parsers/__spec-examples__/schemaprops.ts +++ b/lib/src/parsers/__spec-examples__/schemaprops.ts @@ -22,6 +22,11 @@ type SchemaPropTests = { * "1990-12-31" * */ "property-with-date": Date; + /** property-schemaprop array of integer + * @oaSchemaProp example + * [1990,12,31] + * */ + "property-with-array": Integer[]; /** * @oaSchemaProp example * This_is_not_an_integer diff --git a/lib/src/parsers/schemaprop-parser.spec.ts b/lib/src/parsers/schemaprop-parser.spec.ts index 5bcb7dc88..477f8cbfe 100644 --- a/lib/src/parsers/schemaprop-parser.spec.ts +++ b/lib/src/parsers/schemaprop-parser.spec.ts @@ -86,6 +86,25 @@ describe("schemaprop-parser", () => { ]); }); + test("successfully parses array schemaprops on properties on type ParsedSchemaProps", () => { + const booleanSchemaPropNode = getJsDocsFromPropertySignatures( + properties, + '"property-with-array"' + ); + const retrieveBooleanSchemaProp = extractJSDocSchemaProps( + booleanSchemaPropNode, + { + kind: TypeKind.ARRAY, + elementType: { kind: TypeKind.INT32 } + } + ); + expect(retrieveBooleanSchemaProp).toBeDefined(); + expect(retrieveBooleanSchemaProp!.isOk).toBeTruthy(); + expect(retrieveBooleanSchemaProp!.unwrapOrThrow()).toStrictEqual([ + { name: "example", value: [1990, 12, 31] } + ]); + }); + test("successfully parses date schemaprops on properties on type ParsedSchemaProps", () => { const booleanSchemaPropNode = getJsDocsFromPropertySignatures( properties, diff --git a/lib/src/parsers/schemaprop-parser.ts b/lib/src/parsers/schemaprop-parser.ts index a9ac6719b..7b7156cf2 100644 --- a/lib/src/parsers/schemaprop-parser.ts +++ b/lib/src/parsers/schemaprop-parser.ts @@ -113,39 +113,10 @@ export function extractJSDocSchemaProps( return schemaPropError; } - const typeOf = (value: any): TypeKind | "number" => { - if (/^-?\d+(\.\d+)?$/.test(value)) { - return "number"; - } - - if (typeof value === "boolean") { - return TypeKind.BOOLEAN; - } - - return TypeKind.STRING; - }; - const nameSchemaProps: string[] = schemaProps.map(ex => { return ex.name; }); - if ( - schemaProps.some( - schemaProp => - (propTypeMap.get(schemaProp.name)?.type === "any" && - typeOf(schemaProp.value) !== typeSpecified) || - (propTypeMap.get(schemaProp.name)?.type !== "any" && - propTypeMap.get(schemaProp.name)?.type !== typeOf(schemaProp.value)) - ) - ) { - return err( - new ParserError("property type is wrong or property not allowed", { - file: parentJsDocNode.getSourceFile().getFilePath(), - position: parentJsDocNode.getPos() - }) - ); - } - if ( nameSchemaProps.some( nameSchemaProp => @@ -156,7 +127,9 @@ export function extractJSDocSchemaProps( ) { return err( new ParserError( - "property must be compliant with " + typeSpecified + " type", + "property must be compliant with " + + typeSpecified + + " type or property not allowed", { file: parentJsDocNode.getSourceFile().getFilePath(), position: parentJsDocNode.getPos() @@ -165,6 +138,29 @@ export function extractJSDocSchemaProps( ); } + const typeOf = (value: any): string => { + const typeOfValue = Object.prototype.toString.call(value); + const regex = /\[object |\]/g; + return typeOfValue.replace(regex, "").toLowerCase(); + }; + + if ( + schemaProps.some( + schemaProp => + (propTypeMap.get(schemaProp.name)?.type === "any" && + typeOf(schemaProp.value) !== typeSpecified) || + (propTypeMap.get(schemaProp.name)?.type !== "any" && + propTypeMap.get(schemaProp.name)?.type !== typeOf(schemaProp.value)) + ) + ) { + return err( + new ParserError("property type is wrong", { + file: parentJsDocNode.getSourceFile().getFilePath(), + position: parentJsDocNode.getPos() + }) + ); + } + return ok(schemaProps); } return;