@amritk/generate-validators 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +31 -4
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -675,6 +675,31 @@ var getUnbrandedType = (schema, options = {}) => {
675
675
  case "object":
676
676
  if (schema.properties) {
677
677
  const readonlyPrefix = options.readonly ? "readonly " : "";
678
+ const hasDescriptions = Object.values(schema.properties).some((p) => isSchemaObject2(p) && (typeof p.description === "string" || typeof p.$comment === "string"));
679
+ if (hasDescriptions) {
680
+ let properties2 = "";
681
+ let first2 = true;
682
+ for (const key in schema.properties) {
683
+ const propSchema = schema.properties[key];
684
+ const isRequired = schema.required?.includes(key) ?? false;
685
+ const optional = isRequired ? "" : "?";
686
+ const propType = getTypeScriptType(propSchema, options);
687
+ const inlineDescription = isSchemaObject2(propSchema) && typeof propSchema.description === "string" ? propSchema.description : isSchemaObject2(propSchema) && typeof propSchema.$comment === "string" ? propSchema.$comment : undefined;
688
+ if (!first2)
689
+ properties2 += `
690
+ `;
691
+ first2 = false;
692
+ if (inlineDescription) {
693
+ properties2 += " /** " + inlineDescription + ` */
694
+ ` + readonlyPrefix + safeKey(key) + optional + ": " + propType + ";";
695
+ } else {
696
+ properties2 += " " + readonlyPrefix + safeKey(key) + optional + ": " + propType + ";";
697
+ }
698
+ }
699
+ return `{
700
+ ` + properties2 + `
701
+ }`;
702
+ }
678
703
  let properties = "";
679
704
  let first = true;
680
705
  for (const key in schema.properties) {
@@ -716,8 +741,9 @@ var generateTypeDefinition = (schema, typeName, options = {}) => {
716
741
  if (!isObjectLikeSchema(schema)) {
717
742
  const tsType = getTypeScriptType(schema, options);
718
743
  let result = "";
719
- if (isSchemaObject2(schema) && schema.$comment && typeof schema.$comment === "string") {
720
- result += buildJsDocBlock(typeName, schema.$comment);
744
+ const topLevelComment = isSchemaObject2(schema) && typeof schema.description === "string" && schema.description || isSchemaObject2(schema) && typeof schema.$comment === "string" && schema.$comment || undefined;
745
+ if (topLevelComment) {
746
+ result += buildJsDocBlock(typeName, topLevelComment);
721
747
  }
722
748
  result += `export type ${typeName} = ${tsType};`;
723
749
  return result;
@@ -728,9 +754,10 @@ var generateTypeDefinition = (schema, typeName, options = {}) => {
728
754
  const conditionalThenRef = conditionalResult?.thenRef ?? null;
729
755
  let jsDocTitle;
730
756
  let jsDocDescription;
731
- if (isSchemaObject2(schema) && schema.$comment && typeof schema.$comment === "string") {
757
+ const topLevelComment = isSchemaObject2(schema) && typeof schema.description === "string" && schema.description || isSchemaObject2(schema) && typeof schema.$comment === "string" && schema.$comment || undefined;
758
+ if (topLevelComment) {
732
759
  jsDocTitle = typeName;
733
- jsDocDescription = schema.$comment;
760
+ jsDocDescription = topLevelComment;
734
761
  }
735
762
  const hasProperties2 = normalizedSchema.properties && Object.keys(normalizedSchema.properties).length > 0;
736
763
  const hasAdditionalProperties2 = normalizedSchema.additionalProperties && typeof normalizedSchema.additionalProperties === "object";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/generate-validators",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Generate TypeScript validation functions from JSON Schemas.",
5
5
  "module": "./dist/index.js",
6
6
  "type": "module",
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "json-schema-typed": "^8.0.1",
50
- "@amritk/helpers": "0.6.0"
50
+ "@amritk/helpers": "0.6.1"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@scalar/openapi-parser": "^0.26.1"