Skip to content

Commit

Permalink
airtasker#1417 object-array_example_schemaprop
Browse files Browse the repository at this point in the history
  • Loading branch information
Thodin3 committed May 11, 2021
1 parent 0ffcb38 commit b076b32
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 30 deletions.
5 changes: 5 additions & 0 deletions lib/src/parsers/__spec-examples__/schemaprops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/src/parsers/schemaprop-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
56 changes: 26 additions & 30 deletions lib/src/parsers/schemaprop-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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()
Expand All @@ -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;
Expand Down

0 comments on commit b076b32

Please sign in to comment.