From 3d2fe87e1f9d30947b00aec871724f92d503be15 Mon Sep 17 00:00:00 2001 From: Rowan Cockett Date: Wed, 11 Oct 2023 14:37:22 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Allow=20funding=20IDs=20to=20be?= =?UTF-8?q?=20numeric=20(#657)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .changeset/nine-jobs-leave.md | 5 +++++ .changeset/silver-poems-lick.md | 5 +++++ packages/myst-frontmatter/src/funding/validators.ts | 2 +- packages/simple-validators/src/validators.ts | 8 ++++++-- 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 .changeset/nine-jobs-leave.md create mode 100644 .changeset/silver-poems-lick.md diff --git a/.changeset/nine-jobs-leave.md b/.changeset/nine-jobs-leave.md new file mode 100644 index 000000000..402f5ed1d --- /dev/null +++ b/.changeset/nine-jobs-leave.md @@ -0,0 +1,5 @@ +--- +'simple-validators': patch +--- + +Add coerceNumber to string validation options diff --git a/.changeset/silver-poems-lick.md b/.changeset/silver-poems-lick.md new file mode 100644 index 000000000..38f7d183c --- /dev/null +++ b/.changeset/silver-poems-lick.md @@ -0,0 +1,5 @@ +--- +'myst-frontmatter': patch +--- + +Allow funding award IDs to be numeric, and then be cast to strings. diff --git a/packages/myst-frontmatter/src/funding/validators.ts b/packages/myst-frontmatter/src/funding/validators.ts index 589bf8bc9..a7d55a9f1 100644 --- a/packages/myst-frontmatter/src/funding/validators.ts +++ b/packages/myst-frontmatter/src/funding/validators.ts @@ -28,7 +28,7 @@ export function validateAward(input: any, stash: ReferenceStash, opts: Validatio if (value === undefined) return undefined; const output: Award = {}; if (defined(value.id)) { - output.id = validateString(value.id, incrementOptions('id', opts)); + output.id = validateString(value.id, { ...incrementOptions('id', opts), coerceNumber: true }); } if (defined(value.name)) { output.name = validateString(value.name, incrementOptions('name', opts)); diff --git a/packages/simple-validators/src/validators.ts b/packages/simple-validators/src/validators.ts index 879a53e98..839f09567 100644 --- a/packages/simple-validators/src/validators.ts +++ b/packages/simple-validators/src/validators.ts @@ -90,10 +90,14 @@ export function validateNumber( */ export function validateString( input: any, - opts: { maxLength?: number; regex?: string | RegExp } & ValidationOptions, + opts: { coerceNumber?: boolean; maxLength?: number; regex?: string | RegExp } & ValidationOptions, ): string | undefined { - if (typeof input !== 'string') return validationError(`must be string`, opts); let value = input as string; + if (opts.coerceNumber && typeof value === 'number') { + if (Number.isNaN(value)) validationWarning('is not a number', opts); + value = String(value); + } + if (typeof value !== 'string') return validationError(`must be string`, opts); if (opts.maxLength && value.length > opts.maxLength) { return validationError(`must be less than ${opts.maxLength} chars`, opts); }