From 161b02059f873d47e2c95c7d56a0a7c6aa390041 Mon Sep 17 00:00:00 2001 From: mason Date: Sat, 14 Dec 2024 22:56:28 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20fix:=20give=20Windsurf=20a=20cra?= =?UTF-8?q?ck=20at=20dealing=20with=20JSR's=20restrictions=20(after=20prov?= =?UTF-8?q?iding=20a=20hint)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AlphabeticOrUnderscoreOrHyphenString.ts | 6 +++++- AlphabeticOrUnderscoreString.ts | 6 +++--- AlphabeticString.ts | 6 +++--- AwesomeString.ts | 7 ++++--- LowercaseAlphabeticString.test.ts | 2 +- LowercaseAlphabeticString.ts | 8 ++++---- PrometheusMetricName.ts | 6 +++--- PrometheusMetricNameWithoutColon.ts | 6 +++--- README.md | 6 ++++-- 9 files changed, 30 insertions(+), 23 deletions(-) diff --git a/AlphabeticOrUnderscoreOrHyphenString.ts b/AlphabeticOrUnderscoreOrHyphenString.ts index 5841094..23e184c 100644 --- a/AlphabeticOrUnderscoreOrHyphenString.ts +++ b/AlphabeticOrUnderscoreOrHyphenString.ts @@ -1,7 +1,8 @@ import { ValidatedString } from './ValidatedString.ts'; const isAlphabeticOrUnderscoreOrHyphen = (s: string) => /^[a-zA-Z_-]+$/.test(s); -const { factory, type } = ValidatedString.create( +// Why JSRCompatibleFactory? See https://github.com/axhxrx/validated-string/issues/1 +const JSRCompatibleFactory = ValidatedString.create( isAlphabeticOrUnderscoreOrHyphen, { name: 'AlphabeticOrUnderscoreOrHyphenString', @@ -9,5 +10,8 @@ const { factory, type } = ValidatedString.create( }, ); +const type = JSRCompatibleFactory.type; +const factory = JSRCompatibleFactory.factory; + export type AlphabeticOrUnderscoreOrHyphenString = typeof type; export const AlphabeticOrUnderscoreOrHyphenString = factory; diff --git a/AlphabeticOrUnderscoreString.ts b/AlphabeticOrUnderscoreString.ts index 541c4a2..75c619e 100644 --- a/AlphabeticOrUnderscoreString.ts +++ b/AlphabeticOrUnderscoreString.ts @@ -2,10 +2,10 @@ import { ValidatedString } from './ValidatedString.ts'; const isAlphabeticOrUnderscore = (s: string): boolean => /^[a-zA-Z_]+$/.test(s); -const { factory, type } = ValidatedString.create(isAlphabeticOrUnderscore, { +const JSRCompatibleFactory = ValidatedString.create(isAlphabeticOrUnderscore, { name: 'AlphabeticOrUnderscoreString', description: 'must contain only letters (A-Z, a-z) or underscores', }); -export type AlphabeticOrUnderscoreString = typeof type; -export const AlphabeticOrUnderscoreString = factory; +export type AlphabeticOrUnderscoreString = typeof JSRCompatibleFactory.type; +export const AlphabeticOrUnderscoreString = JSRCompatibleFactory.factory; diff --git a/AlphabeticString.ts b/AlphabeticString.ts index f795563..a812537 100644 --- a/AlphabeticString.ts +++ b/AlphabeticString.ts @@ -2,10 +2,10 @@ import { ValidatedString } from './ValidatedString.ts'; const isAlphabetic = (s: string): boolean => /^[a-zA-Z]+$/.test(s); -const { factory, type } = ValidatedString.create(isAlphabetic, { +const JSRCompatibleFactory = ValidatedString.create(isAlphabetic, { name: 'AlphabeticString', description: 'must contain only letters (A-Z, a-z)', }); -export type AlphabeticString = typeof type; -export const AlphabeticString = factory; +export type AlphabeticString = typeof JSRCompatibleFactory.type; +export const AlphabeticString = JSRCompatibleFactory.factory; diff --git a/AwesomeString.ts b/AwesomeString.ts index 7d5270e..341ce7f 100644 --- a/AwesomeString.ts +++ b/AwesomeString.ts @@ -2,9 +2,10 @@ import { ValidatedString } from './ValidatedString.ts'; const isAwesome = (s: string): boolean => s === 'awesome'; -const { factory, type } = ValidatedString.create(isAwesome); -export type AwesomeString = typeof type; -export const AwesomeString = factory; +const JSRCompatibleFactory = ValidatedString.create(isAwesome); + +export type AwesomeString = typeof JSRCompatibleFactory.type; +export const AwesomeString = JSRCompatibleFactory.factory; // Usage: const x = AwesomeString.try('awesome'); // AwesomeString | undefined diff --git a/LowercaseAlphabeticString.test.ts b/LowercaseAlphabeticString.test.ts index 49f0a4e..e2efb43 100644 --- a/LowercaseAlphabeticString.test.ts +++ b/LowercaseAlphabeticString.test.ts @@ -33,7 +33,7 @@ Deno.test('LowercaseAlphabeticString - assert throws with descriptive message', assertThrows( () => LowercaseAlphabeticString.assert('Hello123!'), Error, - 'Invalid string did not pass validation: Supplied value "Hello123!" is not valid for validator "LowercaseAlphabeticString" (must contain only lowercase letters a-z).', + 'Invalid string did not pass validation: Supplied value "Hello123!" is not valid for validator "LowercaseAlphabeticString" (must contain only lowercase letters (a-z)).', ); }); diff --git a/LowercaseAlphabeticString.ts b/LowercaseAlphabeticString.ts index dabce80..207a20e 100644 --- a/LowercaseAlphabeticString.ts +++ b/LowercaseAlphabeticString.ts @@ -2,10 +2,10 @@ import { ValidatedString } from './ValidatedString.ts'; const isLowercaseAlpha = (s: string): boolean => /^[a-z]+$/.test(s); -const { factory, type } = ValidatedString.create(isLowercaseAlpha, { +const JSRCompatibleFactory = ValidatedString.create(isLowercaseAlpha, { name: 'LowercaseAlphabeticString', - description: 'must contain only lowercase letters a-z', + description: 'must contain only lowercase letters (a-z)', }); -export type LowercaseAlphabeticString = typeof type; -export const LowercaseAlphabeticString = factory; +export type LowercaseAlphabeticString = typeof JSRCompatibleFactory.type; +export const LowercaseAlphabeticString = JSRCompatibleFactory.factory; diff --git a/PrometheusMetricName.ts b/PrometheusMetricName.ts index 8265452..a86ac81 100644 --- a/PrometheusMetricName.ts +++ b/PrometheusMetricName.ts @@ -2,7 +2,7 @@ import { ValidatedString } from './ValidatedString.ts'; const isPrometheusMetricName = (s: string): boolean => /^[a-zA-Z_:][a-zA-Z0-9_:]*$/.test(s); -const { factory, type } = ValidatedString.create(isPrometheusMetricName, { +const JSRCompatibleFactory = ValidatedString.create(isPrometheusMetricName, { name: 'PrometheusMetricName', description: 'must start with a letter, underscore, or colon, followed by letters, digits, underscores, or colons', }); @@ -10,5 +10,5 @@ const { factory, type } = ValidatedString.create(isPrometheusMetricName, { /** A validated string that represents a Prometheus metric name. See {@link ValidatedString} for more information. Note that this might not really exactly match the rules for Prometheus metric names, but it's close enough for our purposes; namely, generating metrics — don't rely on this to *validate* Prometheus metric names, as it is based on a quick skim of the Prometheus docs. */ -export type PrometheusMetricName = typeof type; -export const PrometheusMetricName = factory; +export type PrometheusMetricName = typeof JSRCompatibleFactory.type; +export const PrometheusMetricName = JSRCompatibleFactory.factory; diff --git a/PrometheusMetricNameWithoutColon.ts b/PrometheusMetricNameWithoutColon.ts index 4895478..1446056 100644 --- a/PrometheusMetricNameWithoutColon.ts +++ b/PrometheusMetricNameWithoutColon.ts @@ -2,7 +2,7 @@ import { ValidatedString } from './ValidatedString.ts'; const isPrometheusMetricNameWithoutColon = (s: string): boolean => /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(s); -const { factory, type } = ValidatedString.create( +const JSRCompatibleFactory = ValidatedString.create( isPrometheusMetricNameWithoutColon, { name: 'PrometheusMetricNameWithoutColon', @@ -10,5 +10,5 @@ const { factory, type } = ValidatedString.create( }, ); -export type PrometheusMetricNameWithoutColon = typeof type; -export const PrometheusMetricNameWithoutColon = factory; +export type PrometheusMetricNameWithoutColon = typeof JSRCompatibleFactory.type; +export const PrometheusMetricNameWithoutColon = JSRCompatibleFactory.factory; diff --git a/README.md b/README.md index 73163a2..1f6c5af 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # @axhxrx/validated-strings -A library to help make runtime-validated strings, while still preserving a modicum of type-safety at build-time. Intended for specialized string types that are too complex to be modeled within the type system itself, e.g. "starts with an alphanumeric character and less than 1000 characters long, with at least three swear words", or "three words in German, separated by spaces", etc. +A library to help make runtime-validated strings, but using branded types to preserve a modicum of type-safety and convenience at build-time. + +Intended for specialized string types that are too complex to be modeled within the type system itself, e.g. "starts with an alphanumeric character and less than 1000 characters long, with at least three swear words", or "three words in German, separated by spaces", etc. Example: @@ -23,7 +25,7 @@ export type CowName = typeof type; const x = CowName.try('Bessie'); // x is 'Bessie' const y: CowName | undefined = CowName.try('not a cow name'); // y is undefined const z: CowName = CowName.assert('Bessie'); // z is 'Bessie' -const ohFuck: never = CowName.assert('not a cow name'); // throws an error +const ohFuck = CowName.assert('not a cow name'); // throws an error ``` That's useful when you want to worry about the validation only in one spot, and lean on the type system elsewhere. E.g.: