From 6b64cc43babeeed4770ff191bdeffa4070b99266 Mon Sep 17 00:00:00 2001 From: Cody Olsen Date: Fri, 26 Jan 2024 23:29:00 +0100 Subject: [PATCH] fix(stega): resolve issue where strings that ends in numbers are mistaken for datetimes --- src/stega/filterDefault.ts | 2 +- .../stegaEncodeSourceMap.test.ts.snap | 28 ++++++++++--------- test/stega/stegaEncodeSourceMap.test.ts | 12 +++++++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/stega/filterDefault.ts b/src/stega/filterDefault.ts index 21fb6259..4f34a9bc 100644 --- a/src/stega/filterDefault.ts +++ b/src/stega/filterDefault.ts @@ -100,7 +100,7 @@ const denylist = new Set([ ]) function isValidDate(dateString: string) { - return Number.isNaN(Number(dateString)) ? Boolean(Date.parse(dateString)) : false + return /^\d{4}-\d{2}-\d{2}/.test(dateString) ? Boolean(Date.parse(dateString)) : false } function isValidURL(url: string) { diff --git a/test/stega/__snapshots__/stegaEncodeSourceMap.test.ts.snap b/test/stega/__snapshots__/stegaEncodeSourceMap.test.ts.snap index 9bce882c..8afbc8ec 100644 --- a/test/stega/__snapshots__/stegaEncodeSourceMap.test.ts.snap +++ b/test/stega/__snapshots__/stegaEncodeSourceMap.test.ts.snap @@ -3,6 +3,7 @@ exports[`GraphQL API > decode all 1`] = ` [ "https://test.sanity.studio/intent/edit/mode=presentation;id=9696c833-fe71-45b8-b29d-68e8cd78d3db;type=post;path=title", + "https://test.sanity.studio/intent/edit/mode=presentation;id=ab9e2f38-6ea5-4264-b923-51ae8901ab8e;type=post;path=title", ] `; @@ -14,17 +15,11 @@ exports[`GraphQL API > logger.log 1`] = ` "[@sanity/client/stega]: Encoding source map into result", ], [ - "[@sanity/client/stega]: Paths encoded: 1, skipped: 1", + "[@sanity/client/stega]: Paths encoded: 2, skipped: 0", ], [ "[@sanity/client/stega]: Table of encoded paths", ], - [ - "[@sanity/client/stega]: List of skipped paths", - [ - "title", - ], - ], ] `; @@ -37,6 +32,11 @@ exports[`GraphQL API > logger.table 1`] = ` "path": "title", "value": "Stay tuned for detai...", }, + { + "length": 28, + "path": "title", + "value": "Anticipation is buil...", + }, ], ], ] @@ -73,7 +73,7 @@ exports[`resolveEditUrl '/' > logger.log 1`] = ` "[@sanity/client/stega]: Encoding source map into result", ], [ - "[@sanity/client/stega]: Paths encoded: 18, skipped: 80", + "[@sanity/client/stega]: Paths encoded: 18, skipped: 81", ], [ "[@sanity/client/stega]: Table of encoded paths", @@ -91,6 +91,7 @@ exports[`resolveEditUrl '/' > logger.log 1`] = ` "media[].asset._ref", "media[].asset._type", "_id", + "lastModified", ], ], ] @@ -176,9 +177,9 @@ exports[`resolveEditUrl '/' > logger.table 1`] = ` "value": "Meet Ripple, a new s...", }, { - "length": 13, + "length": 34, "path": "title", - "value": "Ripple Sconce", + "value": "Ripple 1996-12-20T00...", }, { "length": 4, @@ -226,7 +227,7 @@ exports[`resolveEditUrl 'https://test.sanity.studio' > logger.log 1`] = ` "[@sanity/client/stega]: Encoding source map into result", ], [ - "[@sanity/client/stega]: Paths encoded: 18, skipped: 80", + "[@sanity/client/stega]: Paths encoded: 18, skipped: 81", ], [ "[@sanity/client/stega]: Table of encoded paths", @@ -244,6 +245,7 @@ exports[`resolveEditUrl 'https://test.sanity.studio' > logger.log 1`] = ` "media[].asset._ref", "media[].asset._type", "_id", + "lastModified", ], ], ] @@ -329,9 +331,9 @@ exports[`resolveEditUrl 'https://test.sanity.studio' > logger.table 1`] = ` "value": "Meet Ripple, a new s...", }, { - "length": 13, + "length": 34, "path": "title", - "value": "Ripple Sconce", + "value": "Ripple 1996-12-20T00...", }, { "length": 4, diff --git a/test/stega/stegaEncodeSourceMap.test.ts b/test/stega/stegaEncodeSourceMap.test.ts index d1b3bb69..2c1073b7 100644 --- a/test/stega/stegaEncodeSourceMap.test.ts +++ b/test/stega/stegaEncodeSourceMap.test.ts @@ -225,12 +225,13 @@ const mock = { }, }, _id: 'e1bf9f1f-efdb-4105-8c26-6b64f897e9c1', - title: 'Ripple Sconce', + title: 'Ripple 1996-12-20T00:39:57Z Sconce', }, ], siteSettings: { title: 'acme', copyrightText: 'acme © 2023 — all rights reserved', + lastModified: '2024-01-01', }, }, resultSourceMap: { @@ -271,6 +272,7 @@ const mock = { "$['media'][?(@._key=='3239041c90c8')]", "$['media'][?(@._key=='c3c15f50cc9b')]", "$['copyrightText']", + "$['lastModified']", ], mappings: { "$['products'][0]['_id']": { @@ -489,6 +491,14 @@ const mock = { }, type: 'value', }, + "$['siteSettings']['lastModified']": { + source: { + document: 5, + path: 10, + type: 'documentValue', + }, + type: 'value', + }, }, }, ms: 40,