-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nominal-typebox): add brandedRegExp
Signed-off-by: Andres Correa Casablanca <andreu@kindspells.dev>
- Loading branch information
Showing
12 changed files
with
159 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../.node-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { FastBrand } from '@coderspirit/nominal' | ||
import type { Kind, RegExpOptions, TSchema } from '@sinclair/typebox' | ||
import { RegExp as TBRegExp } from '@sinclair/typebox' | ||
|
||
export interface BrandedRegExpSchema<B extends string> extends TSchema { | ||
// Copied from TRegExp | ||
[Kind]: 'RegExp' | ||
type: 'RegExp' | ||
source: string | ||
flags: string | ||
|
||
// Our sauce | ||
static: FastBrand<string, B> | ||
} | ||
|
||
function brandedRegExp<const B extends string>( | ||
regex: RegExp, | ||
options?: RegExpOptions, | ||
): BrandedRegExpSchema<B> | ||
function brandedRegExp<const B extends string>( | ||
regex: string, | ||
options?: RegExpOptions, | ||
): BrandedRegExpSchema<B> | ||
function brandedRegExp<const B extends string>( | ||
pattern: string | RegExp, | ||
options?: RegExpOptions, | ||
): BrandedRegExpSchema<B> { | ||
return TBRegExp(pattern as string, options) as BrandedRegExpSchema<B> | ||
} | ||
|
||
export { brandedRegExp } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import assert from 'node:assert/strict' | ||
|
||
import type { FastBrand } from '@coderspirit/nominal' | ||
import { TypeCompiler } from '@sinclair/typebox/compiler' | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { brandedRegExp } from '../main.mjs' | ||
|
||
describe('brandedRegExp', () => { | ||
it('lets typebox to annotate a regexp with a brand', () => { | ||
const regexpSchema = brandedRegExp<'UUID'>( | ||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/, | ||
) | ||
const regexpValidator = TypeCompiler.Compile(regexpSchema) | ||
|
||
const valueToCheck = '09e77e4a-175d-46a1-b6c6-ff960c30193b' | ||
if (!regexpValidator.Check(valueToCheck)) { | ||
throw new assert.AssertionError({ message: 'validation should pass' }) | ||
} | ||
|
||
const regexpSink: FastBrand<string, 'UUID'> = valueToCheck | ||
expect(regexpSink).toBe('09e77e4a-175d-46a1-b6c6-ff960c30193b') | ||
|
||
// We perform the following useless assignments to show the contrast between | ||
// tagged values and untagged values. | ||
|
||
// @ts-expect-error | ||
const corruptedRegexpSink: FastBrand<string, 'UUID'> = | ||
'09e77e4a-175d-46a1-b6c6-ff960c30193b' | ||
expect(corruptedRegexpSink).toBe('09e77e4a-175d-46a1-b6c6-ff960c30193b') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.