Skip to content

Commit

Permalink
fix(cli): enforce date validation on date-type examples (#4358)
Browse files Browse the repository at this point in the history
* fix(cli): enforce date validation on date-type examples

* different date definition, oops

* release ready

* update changelog
  • Loading branch information
neha-goswami authored Aug 21, 2024
1 parent 4e7e852 commit 8e12d32
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/cli/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.39.14] - 2024-08-21

- Fix: Format validation is enforced on `date` fields that are specified in examples specified in an api defintion.

## [0.39.13] - 2024-08-19

- Fix: Generated examples in the Intermediate Representation not respect root level path parameter examples. Previously, when ignored,
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/cli/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.39.13
0.39.14
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const ISO_8601_REGEX =
// https://ihateregex.io/expr/uuid/
const UUID_REGEX = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/;

const RFC_3339_DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/;

export function validateTypeReferenceExample({
rawTypeReference,
example,
Expand Down Expand Up @@ -244,9 +246,9 @@ function validatePrimitiveExample({
example,
rules: v2.validation
}),
uuid: () => validateString(example),
uuid: () => validateUuid(example),
date: () => validateDate(example),
dateTime: () => validateDateTime(example),
date: () => validateString(example),
base64: () => validateString(example),
bigInteger: () => validateString(example),
_other: () => {
Expand All @@ -265,7 +267,7 @@ function validatePrimitiveExample({
string: () => validateString(example),
uuid: () => validateUuid(example),
dateTime: () => validateDateTime(example),
date: () => validateString(example),
date: () => validateDate(example),
base64: () => validateString(example),
bigInteger: () => validateString(example),
_other: () => {
Expand Down Expand Up @@ -421,6 +423,10 @@ const validateDateTime = createValidator(
(example) => typeof example === "string" && ISO_8601_REGEX.test(example),
"an ISO 8601 timestamp"
);
const validateDate = createValidator(
(example) => typeof example === "string" && RFC_3339_DATE_REGEX.test(example) && ISO_8601_REGEX.test(example),
"a date"
);

function createValidator(
validate: (example: RawSchemas.ExampleTypeReferenceSchema) => boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ types:
- value: true
- value: 100
- value: false
DateAlias:
type: date
examples:
- value: "2024-01-01"
- value: "2024-01-35"
- value: "2024-01-01T00:00:00Z"
- value: "2024-1-1"
DateTimeAlias:
type: datetime
examples:
- value: 4/13/2002
- value: "2022-12-06T23:13:15+00:00"
- value: "2022-12-06T23:13:15Z"
- value: "20221206T231315Z"
- value: "2024-01-01"
UnknownAlias:
type: unknown
examples:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,24 @@ describe("valid-example-type", () => {
message: "Expected example to be a boolean. Example is: 100",
nodePath: ["types", "BooleanAlias", { key: "examples", arrayIndex: 1 }]
},
{
severity: "error",
relativeFilepath: RelativeFilePath.of("alias.yml"),
message: 'Expected example to be a date. Example is: "2024-01-35"',
nodePath: ["types", "DateAlias", { key: "examples", arrayIndex: 1 }]
},
{
severity: "error",
relativeFilepath: RelativeFilePath.of("alias.yml"),
message: 'Expected example to be a date. Example is: "2024-01-01T00:00:00Z"',
nodePath: ["types", "DateAlias", { key: "examples", arrayIndex: 2 }]
},
{
severity: "error",
relativeFilepath: RelativeFilePath.of("alias.yml"),
message: 'Expected example to be a date. Example is: "2024-1-1"',
nodePath: ["types", "DateAlias", { key: "examples", arrayIndex: 3 }]
},
{
severity: "error",
relativeFilepath: RelativeFilePath.of("alias.yml"),
Expand Down
2 changes: 0 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8e12d32

Please sign in to comment.