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); }