Skip to content

Commit

Permalink
Forbid unexpected fields in yaml frontmatter (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy authored Mar 20, 2024
1 parent 3b0c37e commit 718eb0f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CGPs/cgp-0039.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cgp: 39
title: Increase Block Target Density
date-created: 2021-09-06
author: 'Mariano Cortesi (@mcortesi), Or Neeman (@oneeman)'
discussionts-to: https://forum.celo.org/t/discussion-for-cgp-0039-incre/1333
discussions-to: https://forum.celo.org/t/discussion-for-cgp-0039-incre/1333
status: EXECUTED
date-executed:
governance-proposal-id: 39
Expand Down
3 changes: 1 addition & 2 deletions CGPs/cgp-0087.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
---
cgp: 87
title: Temperature Check - Celo transition to an Ethereum L2
date created: 2023-07-21
date-created: 2023-07-21
author: productmatt
discussions-to: https://forum.celo.org/t/clabs-proposal-for-celo-to-transition-to-an-ethereum-l2/6109
status: EXECUTED
governance-proposal-id: 116
type: Temparature Check
---

# Abstract
Expand Down
2 changes: 1 addition & 1 deletion CGPs/cgp-0127.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Enable USDC as Gas Currency
date-created: 2024-05-18
author: Martín Volpe (@martinvol), Pavel Hornak (@pahor167)
status: PROPOSED
Discussions-to: https://forum.celo.org/t/path-towards-making-usdc-a-gas-currency/7385/2
discussions-to: https://forum.celo.org/t/path-towards-making-usdc-a-gas-currency/7385/2
governance-proposal-id: 168
date-executed:
---
Expand Down
51 changes: 34 additions & 17 deletions scripts/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function validateCGPs() {
}
}

console.log('Done validating CGPs');
console.log("Done validating CGPs");
}

function validateCgpFolder(pathName) {
Expand All @@ -39,7 +39,7 @@ function validateCgpFolder(pathName) {
}

function validateCgpFile(fileName) {
if (fileName === CGP_TEMPLATE_FILENAME) return
if (fileName === CGP_TEMPLATE_FILENAME) return;

if (!CGP_FILENAME_REGEX.test(fileName)) {
throw new Error(`Invalid CGP filename: ${fileName}`);
Expand Down Expand Up @@ -85,24 +85,41 @@ const ProposalMetadataStatus = {
/**
* The schema as used in the CGP front matter
*/
const DateString = z.string().regex(/^\d{4}-\d{2}-\d{2}$/)

export const ProposalMetadataSchema = z.object({
cgp: z.number().min(1),
title: z.string().min(1),
author: z.string().min(1),
status: z.nativeEnum(ProposalMetadataStatus),
"date-created": DateString.optional().or(z.null()),
"discussions-to": z.string().url().optional().or(z.null()),
"governance-proposal-id": z.number().min(1).optional().or(z.null()),
"date-executed": DateString.optional().or(z.null()),
});
const DateString = z.string().regex(/^\d{4}-\d{2}-\d{2}$/);

export const ProposalMetadataSchema = z
.object({
cgp: z.number().min(1),
title: z.string().min(1),
author: z.string().min(1),
status: z.nativeEnum(ProposalMetadataStatus),
"date-created": DateString.optional().or(z.null()),
"discussions-to": z.string().url().optional().or(z.null()),
"governance-proposal-id": z.number().min(1).optional().or(z.null()),
"date-executed": DateString.optional().or(z.null()),
})
.strict();

function validateFrontMatter(data, filename) {
try {
const parsed = ProposalMetadataSchema.parse(data);
parsed["date-created"] && validateDate(parsed["date-created"])
parsed["date-executed"] && validateDate(parsed["date-executed"])
const {
status,
"governance-proposal-id": proposalId,
"date-created": dateCreated,
"date-executed": dateExecuted,
} = parsed;
if (
![
ProposalMetadataStatus.DRAFT,
ProposalMetadataStatus.WITHDRAWN,
].includes(status) &&
!proposalId
) {
throw new Error(`Proposal ID required for non-draft CGPs`);
}
if (dateCreated) validateDate(dateCreated);
if (dateExecuted) validateDate(dateExecuted);
} catch (error) {
console.error("Error validating front matter", error);
throw new Error(`Error validating front matter: ${filename}`);
Expand All @@ -111,7 +128,7 @@ function validateFrontMatter(data, filename) {

function validateDate(value) {
const date = new Date(value);
if (date instanceof Date && !isNaN(date.getTime())) return
if (date instanceof Date && !isNaN(date.getTime())) return;
throw new Error(`Invalid date: ${value}`);
}

Expand Down

0 comments on commit 718eb0f

Please sign in to comment.