Skip to content

Commit b30267c

Browse files
Fixing broken field bug (#104)
* add .or literal addition * adding verbose error logging * editing code.json to trigger validation * changing version for action * manually editing code.json schema to trigger fail * adding package to handle errors * reverting things
1 parent aa5ed74 commit b30267c

7 files changed

Lines changed: 38 additions & 9 deletions

File tree

.github/workflows/updateCodeJSON.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222

2323
- name: Update code.json
2424
id: generator
25-
uses: DSACMS/automated-codejson-generator@sachin/inheriting-from-source-model
25+
uses: DSACMS/automated-codejson-generator@main
2626
with:
2727
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2828
ADMIN_TOKEN: ${{ secrets.ADMIN_PAT }}

code.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,4 @@
6868
"subsetInHealthcare": ["operational"],
6969
"userType": ["government"],
7070
"maturityModelTier": 3
71-
}
71+
}

package-lock.json

Lines changed: 14 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
"@octokit/action": "^7.0.0",
4343
"json-schema-to-zod": "^2.7.0",
4444
"octokit-plugin-create-pull-request": "^6.0.0",
45-
"zod": "^4.2.1"
45+
"zod": "^4.2.1",
46+
"zod-validation-error": "^5.0.0"
4647
},
4748
"devDependencies": {
4849
"@eslint/compat": "^1.2.6",

src/scripts/generate-schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const schemaVersion = "2.0.0";
77
const schemaURL = `https://raw.githubusercontent.com/DSACMS/gov-codejson/refs/heads/main/schemas/cms/schema-${schemaVersion}.json`;
88
const filePath = "src/types/CodeJSONSchema.ts";
99

10+
function allowEmptyUrls(zodCode: string): string {
11+
// allow empty strings for all URL fields while still validating non-empty values
12+
return zodCode.replace(/\.url\(\)/g, '.url().or(z.literal(""))');
13+
}
14+
1015
function fixUniqueArrays(zodCode: string): string {
1116
// replace .unique() with .refine() pattern since Zod doesn't have .unique() for arrays but converter adds it in there
1217
return zodCode.replace(
@@ -71,6 +76,7 @@ async function generateSchema() {
7176
let zodSourceCode = jsonSchemaToZod(jsonSchema);
7277

7378
zodSourceCode = fixUniqueArrays(zodSourceCode);
79+
zodSourceCode = allowEmptyUrls(zodSourceCode)
7480

7581
const fileContent = `
7682
// DO NOT EDIT - AUTOMATICALLY GENERATED FILE!!!

src/types/CodeJSONSchema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export const CodeJSONSchema = z
5757
URL: z
5858
.string()
5959
.url()
60+
.or(z.literal(""))
6061
.describe("The URL of the release license in the repository"),
6162
})
6263
.strict(),
@@ -115,6 +116,7 @@ export const CodeJSONSchema = z
115116
repositoryURL: z
116117
.string()
117118
.url()
119+
.or(z.literal(""))
118120
.describe(
119121
"The URL of the public release repository for open source repositories. This field is not required for repositories that are only available as government-wide reuse or are closed (pursuant to one of the exemptions). It can be listed as 'private' for repositories that are closed.",
120122
),
@@ -134,16 +136,19 @@ export const CodeJSONSchema = z
134136
homepageURL: z
135137
.string()
136138
.url()
139+
.or(z.literal(""))
137140
.describe("The URL of the public release homepage.")
138141
.optional(),
139142
downloadURL: z
140143
.string()
141144
.url()
145+
.or(z.literal(""))
142146
.describe("The URL where a distribution of the release can be found.")
143147
.optional(),
144148
disclaimerURL: z
145149
.string()
146150
.url()
151+
.or(z.literal(""))
147152
.describe(
148153
"The URL where disclaimer language regarding the release can be found.",
149154
)
@@ -236,6 +241,7 @@ export const CodeJSONSchema = z
236241
URL: z
237242
.string()
238243
.url()
244+
.or(z.literal(""))
239245
.describe(
240246
"The URL where the code repository, project, library or release can be found.",
241247
)
@@ -264,6 +270,7 @@ export const CodeJSONSchema = z
264270
URL: z
265271
.string()
266272
.url()
273+
.or(z.literal(""))
267274
.describe("The URL where the software can be found.")
268275
.optional(),
269276
})
@@ -336,6 +343,7 @@ export const CodeJSONSchema = z
336343
feedbackMechanism: z
337344
.string()
338345
.url()
346+
.or(z.literal(""))
339347
.describe(
340348
"Method a repository receives feedback from the community (i.e. URL to GitHub repository issues page)",
341349
),

src/zod-validation.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { z } from "zod";
2+
import { createErrorMap } from "zod-validation-error";
23
import { CodeJSONSchema } from "./types/CodeJSONSchema.js";
34

5+
z.config({
6+
customError: createErrorMap(),
7+
});
8+
49
export function validateCodeJSON(codeJSON: any): string[] {
510
const result = CodeJSONSchema.safeParse(codeJSON);
611

712
if (result.success) {
813
return [];
914
}
1015

11-
return result.error.issues.map((err: z.ZodIssue) => {
12-
const path = err.path.join(".");
13-
const field = path || "root";
14-
return `${field}: ${err.message}`;
15-
});
16+
return [z.prettifyError(result.error)]
1617
}
1718

0 commit comments

Comments
 (0)