Skip to content

Commit

Permalink
🧪 Allow funding IDs to be numeric (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 authored Oct 11, 2023
1 parent 0e7a489 commit 3d2fe87
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-jobs-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'simple-validators': patch
---

Add coerceNumber to string validation options
5 changes: 5 additions & 0 deletions .changeset/silver-poems-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'myst-frontmatter': patch
---

Allow funding award IDs to be numeric, and then be cast to strings.
2 changes: 1 addition & 1 deletion packages/myst-frontmatter/src/funding/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 6 additions & 2 deletions packages/simple-validators/src/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 3d2fe87

Please sign in to comment.