Skip to content

Commit

Permalink
feat(*): use scalar validator regex as pattern value
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Casey <[email protected]>
  • Loading branch information
jonathan-casey committed Nov 6, 2023
1 parent 5f410ab commit a458152
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
14 changes: 12 additions & 2 deletions lib/codegen/fromcto/jsonschema/jsonschemavisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ class JSONSchemaVisitor {
} else if (ModelUtil.isMap?.(field)) {
const mapDeclaration = field.getParent().getModelFile().getModelManager().getType(field.getFullyQualifiedTypeName());

let valueDeclaration = mapDeclaration.getModelFile().getType(mapDeclaration.getValue().getType());
let mapKey = mapDeclaration.getModelFile().getType(mapDeclaration.getKey().getType());
let mapValue = mapDeclaration.getModelFile().getType(mapDeclaration.getValue().getType());

const jsonSchema = {
$id: field.getFullyQualifiedName(),
Expand All @@ -428,8 +429,17 @@ class JSONSchemaVisitor {
}
};

if (mapKey.isScalarDeclaration?.() && mapKey.getValidator()?.getRegex()) {
jsonSchema.schema.propertyNames.pattern = String(mapKey.getValidator().getRegex());

}

if (mapValue.isScalarDeclaration?.() && mapValue.getValidator()?.getRegex()) {
jsonSchema.schema.additionalProperties.pattern = String(mapValue.getValidator().getRegex());
}

// if its a ClassDeclaration, add reference to its schema.
if (valueDeclaration?.isClassDeclaration?.()) {
if (mapValue?.isClassDeclaration?.()) {
jsonSchema.schema.additionalProperties = {$ref: `#/definitions/${field.getParent().getModelFile().getNamespace().concat(`.${mapDeclaration.getValue().getType()}`)}`};
}

Expand Down
36 changes: 24 additions & 12 deletions test/codegen/__snapshots__/codegen.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1706,7 +1706,8 @@ exports[`codegen #formats check we can convert all formats from namespace unvers
"description": "An instance of org.acme.hr.EmployeeDirectory",
"type": "object",
"propertyNames": {
"type": "string"
"type": "string",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
},
"additionalProperties": {
"$ref": "#/definitions/org.acme.hr.Employee"
Expand All @@ -1720,7 +1721,8 @@ exports[`codegen #formats check we can convert all formats from namespace unvers
"description": "An instance of org.acme.hr.base.EmployeeTShirtSizes",
"type": "object",
"propertyNames": {
"type": "string"
"type": "string",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
},
"additionalProperties": {
"$ref": "#/definitions/org.acme.hr.TShirtSizeType"
Expand Down Expand Up @@ -1751,7 +1753,8 @@ exports[`codegen #formats check we can convert all formats from namespace unvers
"type": "string"
},
"additionalProperties": {
"type": "SSN"
"type": "SSN",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
}
}
}
Expand Down Expand Up @@ -2951,7 +2954,8 @@ exports[`codegen #formats check we can convert all formats from namespace unvers
"description": "An instance of org.acme.hr.EmployeeDirectory",
"type": "object",
"propertyNames": {
"type": "string"
"type": "string",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
},
"additionalProperties": {
"$ref": "#/definitions/org.acme.hr.Employee"
Expand All @@ -2965,7 +2969,8 @@ exports[`codegen #formats check we can convert all formats from namespace unvers
"description": "An instance of org.acme.hr.base.EmployeeTShirtSizes",
"type": "object",
"propertyNames": {
"type": "string"
"type": "string",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
},
"additionalProperties": {
"$ref": "#/definitions/org.acme.hr.TShirtSizeType"
Expand Down Expand Up @@ -2996,7 +3001,8 @@ exports[`codegen #formats check we can convert all formats from namespace unvers
"type": "string"
},
"additionalProperties": {
"type": "SSN"
"type": "SSN",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
}
}
}
Expand Down Expand Up @@ -7506,7 +7512,8 @@ exports[`codegen #formats check we can convert all formats from namespace versio
"description": "An instance of [email protected]",
"type": "object",
"propertyNames": {
"type": "string"
"type": "string",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
},
"additionalProperties": {
"$ref": "#/definitions/[email protected]"
Expand All @@ -7520,7 +7527,8 @@ exports[`codegen #formats check we can convert all formats from namespace versio
"description": "An instance of [email protected]",
"type": "object",
"propertyNames": {
"type": "string"
"type": "string",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
},
"additionalProperties": {
"$ref": "#/definitions/[email protected]"
Expand Down Expand Up @@ -7551,7 +7559,8 @@ exports[`codegen #formats check we can convert all formats from namespace versio
"type": "string"
},
"additionalProperties": {
"type": "SSN"
"type": "SSN",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
}
}
}
Expand Down Expand Up @@ -8767,7 +8776,8 @@ exports[`codegen #formats check we can convert all formats from namespace versio
"description": "An instance of [email protected]",
"type": "object",
"propertyNames": {
"type": "string"
"type": "string",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
},
"additionalProperties": {
"$ref": "#/definitions/[email protected]"
Expand All @@ -8781,7 +8791,8 @@ exports[`codegen #formats check we can convert all formats from namespace versio
"description": "An instance of [email protected]",
"type": "object",
"propertyNames": {
"type": "string"
"type": "string",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
},
"additionalProperties": {
"$ref": "#/definitions/[email protected]"
Expand Down Expand Up @@ -8812,7 +8823,8 @@ exports[`codegen #formats check we can convert all formats from namespace versio
"type": "string"
},
"additionalProperties": {
"type": "SSN"
"type": "SSN",
"pattern": "/\\\\d{3}-\\\\d{2}-\\\\{4}+/"
}
}
}
Expand Down

0 comments on commit a458152

Please sign in to comment.