-
-
Notifications
You must be signed in to change notification settings - Fork 369
Implement code points validator #888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tats-u
wants to merge
33
commits into
open-circle:main
Choose a base branch
from
tats-u:code-points
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
b15edab
Implement code points validator
tats-u 27b88b3
Simplify
tats-u 71b4dd2
Reduce count operations
tats-u f724c33
'~validate' => '~run' & format
tats-u abec9d1
Merge branch 'main' into code-points
fabian-hiller 88fa1e0
Add docs
tats-u 0ec38d6
Add notCodePoints
tats-u e06ec84
Add docs for types
tats-u fa19dac
Merge branch 'main' into code-points
yslpn dca7b64
Apply easy-to-fix obvious suggestions from code review
tats-u 84a6bdd
Add missing `// @__NO_SIDE_EFFECTS__`
tats-u a1327f1
Remove duplicated file name suffix
tats-u f781906
Skip check at last code unit
tats-u 3160d12
Add comments
tats-u 726647b
Remove extra import
tats-u ef0a0dc
Move `@__NO_SIDE_EFFECTS__`
tats-u 0637c96
Fix wrong hrefs
tats-u e369008
Fix wrong schemas
tats-u 654b8f4
Fix grammers
tats-u c52ffc2
Fix indent inconsistency
tats-u 5427714
Fix interface JSDoc
tats-u 6398c4b
Change codePoints to code points in JSDoc
tats-u 5a2aafa
Change type to interface in JSDoc
tats-u d4d8c0a
Fix "invalid ..." label
tats-u 44f491d
Update docs
tats-u 6846d19
Merge branch 'main' into code-points
yslpn fcf3141
docs: update API documentation to include codePoints and improve desc…
yslpn 378c2e5
fix: correct issue type string from 'code_points' to 'code points'
yslpn 3e8028f
docs: enhance explanations for code points validation actions
yslpn ee49c4d
feat: add code points actions support for to-json-schema
yslpn f64f7ed
fix(to-json-schema): preserve composed action constraints
yslpn 112a73d
Revert "fix(to-json-schema): preserve composed action constraints"
yslpn 0cb49aa
Revert "feat: add code points actions support for to-json-schema"
yslpn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,50 @@ | ||
| import { describe, expectTypeOf, test } from 'vitest'; | ||
| import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts'; | ||
| import { | ||
| codePoints, | ||
| type CodePointsAction, | ||
| type CodePointsIssue, | ||
| } from './codePoints.ts'; | ||
|
|
||
| describe('codePoints', () => { | ||
| describe('should return action object', () => { | ||
| test('with undefined message', () => { | ||
| type Action = CodePointsAction<string, 10, undefined>; | ||
| expectTypeOf(codePoints<string, 10>(10)).toEqualTypeOf<Action>(); | ||
| expectTypeOf( | ||
| codePoints<string, 10, undefined>(10, undefined) | ||
| ).toEqualTypeOf<Action>(); | ||
| }); | ||
|
|
||
| test('with string message', () => { | ||
| expectTypeOf( | ||
| codePoints<string, 10, 'message'>(10, 'message') | ||
| ).toEqualTypeOf<CodePointsAction<string, 10, 'message'>>(); | ||
| }); | ||
|
|
||
| test('with function message', () => { | ||
| expectTypeOf( | ||
| codePoints<string, 10, () => string>(10, () => 'message') | ||
| ).toEqualTypeOf<CodePointsAction<string, 10, () => string>>(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should infer correct types', () => { | ||
| type Input = 'example string'; | ||
| type Action = CodePointsAction<Input, 5, undefined>; | ||
|
|
||
| test('of input', () => { | ||
| expectTypeOf<InferInput<Action>>().toEqualTypeOf<Input>(); | ||
| }); | ||
|
|
||
| test('of output', () => { | ||
| expectTypeOf<InferOutput<Action>>().toEqualTypeOf<Input>(); | ||
| }); | ||
|
|
||
| test('of issue', () => { | ||
| expectTypeOf<InferIssue<Action>>().toEqualTypeOf< | ||
| CodePointsIssue<Input, 5> | ||
| >(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,121 @@ | ||
| import { describe, expect, test } from 'vitest'; | ||
| import type { StringIssue } from '../../schemas/index.ts'; | ||
| import { _getCodePointCount } from '../../utils/index.ts'; | ||
| import { expectActionIssue, expectNoActionIssue } from '../../vitest/index.ts'; | ||
| import { | ||
| codePoints, | ||
| type CodePointsAction, | ||
| type CodePointsIssue, | ||
| } from './codePoints.ts'; | ||
|
|
||
| describe('codePoints', () => { | ||
| describe('should return action object', () => { | ||
| const baseAction: Omit<CodePointsAction<string, 5, never>, 'message'> = { | ||
| kind: 'validation', | ||
| type: 'code_points', | ||
| reference: codePoints, | ||
| expects: '5', | ||
| requirement: 5, | ||
| async: false, | ||
| '~run': expect.any(Function), | ||
| }; | ||
|
|
||
| test('with undefined message', () => { | ||
| const action: CodePointsAction<string, 5, undefined> = { | ||
| ...baseAction, | ||
| message: undefined, | ||
| }; | ||
| expect(codePoints(5)).toStrictEqual(action); | ||
| expect(codePoints(5, undefined)).toStrictEqual(action); | ||
| }); | ||
|
|
||
| test('with string message', () => { | ||
| expect(codePoints(5, 'message')).toStrictEqual({ | ||
| ...baseAction, | ||
| message: 'message', | ||
| } satisfies CodePointsAction<string, 5, string>); | ||
| }); | ||
|
|
||
| test('with function message', () => { | ||
| const message = () => 'message'; | ||
| expect(codePoints(5, message)).toStrictEqual({ | ||
| ...baseAction, | ||
| message, | ||
| } satisfies CodePointsAction<string, 5, typeof message>); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should return dataset without issues', () => { | ||
| const action = codePoints(5); | ||
|
|
||
| test('for untyped inputs', () => { | ||
| const issues: [StringIssue] = [ | ||
| { | ||
| kind: 'schema', | ||
| type: 'string', | ||
| input: null, | ||
| expected: 'string', | ||
| received: 'null', | ||
| message: 'message', | ||
| }, | ||
| ]; | ||
| expect( | ||
| action['~run']({ typed: false, value: null, issues }, {}) | ||
| ).toStrictEqual({ | ||
| typed: false, | ||
| value: null, | ||
| issues, | ||
| }); | ||
| }); | ||
|
|
||
| test('for valid strings', () => { | ||
| expectNoActionIssue(action, ['12345', '12 45', '1234 ', 'hello']); | ||
| }); | ||
|
|
||
| test('for valid emoji', () => { | ||
| expectNoActionIssue(action, ['👨🏽👩🏽', '😶🌫️😀', '😡👍😁😂😀', '0️⃣㊙️']); | ||
| }); | ||
|
|
||
| test('for valid non-latin', () => { | ||
| expectNoActionIssue(action, ['あ𛀙よろし', '𠮷野家で𩸽', '葛󠄀城市!']); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should return dataset with issues', () => { | ||
| const action = codePoints(5, 'message'); | ||
| const baseIssue: Omit<CodePointsIssue<string, 5>, 'input' | 'received'> = { | ||
| kind: 'validation', | ||
| type: 'code_points', | ||
| expected: '5', | ||
| message: 'message', | ||
| requirement: 5, | ||
| }; | ||
|
|
||
| test('for invalid strings', () => { | ||
| expectActionIssue( | ||
| action, | ||
| baseIssue, | ||
| ['', ' ', '1', '1234', '123 ', '123456', '12 456', '123456789'], | ||
| (value) => `${_getCodePointCount(value)}` | ||
| ); | ||
| }); | ||
|
|
||
| test('for invalid emoji', () => { | ||
| expectActionIssue( | ||
| action, | ||
| baseIssue, | ||
| ['😀👋🏼🧩👩🏻🏫🫥', '㊙️㊙️0️⃣1️⃣2️⃣'], | ||
| (value) => `${_getCodePointCount(value)}` | ||
| ); | ||
| }); | ||
|
|
||
| test('for invalid non-latin', () => { | ||
| expectActionIssue( | ||
| action, | ||
| baseIssue, | ||
| ['竈門禰󠄀豆子', '葛󠄀城市'], | ||
| (value) => `${_getCodePointCount(value)}` | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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,133 @@ | ||
| import type { | ||
| BaseIssue, | ||
| BaseValidation, | ||
| ErrorMessage, | ||
| } from '../../types/index.ts'; | ||
| import { _addIssue, _getCodePointCount } from '../../utils/index.ts'; | ||
|
|
||
| /** | ||
| * Code points issue interface. | ||
| */ | ||
| export interface CodePointsIssue< | ||
| TInput extends string, | ||
| TRequirement extends number, | ||
| > extends BaseIssue<TInput> { | ||
| /** | ||
| * The issue kind. | ||
| */ | ||
| readonly kind: 'validation'; | ||
| /** | ||
| * The issue type. | ||
| */ | ||
| readonly type: 'code_points'; | ||
| /** | ||
| * The expected property. | ||
| */ | ||
| readonly expected: `${TRequirement}`; | ||
| /** | ||
| * The received property. | ||
| */ | ||
| readonly received: `${number}`; | ||
| /** | ||
| * The required code points. | ||
| */ | ||
|
tats-u marked this conversation as resolved.
|
||
| readonly requirement: TRequirement; | ||
| } | ||
|
|
||
| /** | ||
| * Code points action interface. | ||
| */ | ||
|
tats-u marked this conversation as resolved.
|
||
| export interface CodePointsAction< | ||
| TInput extends string, | ||
| TRequirement extends number, | ||
| TMessage extends | ||
| | ErrorMessage<CodePointsIssue<TInput, TRequirement>> | ||
| | undefined, | ||
| > extends BaseValidation< | ||
| TInput, | ||
| TInput, | ||
| CodePointsIssue<TInput, TRequirement> | ||
| > { | ||
| /** | ||
| * The action type. | ||
| */ | ||
| readonly type: 'code_points'; | ||
| /** | ||
| * The action reference. | ||
| */ | ||
| readonly reference: typeof codePoints; | ||
| /** | ||
| * The expected property. | ||
| */ | ||
| readonly expects: `${TRequirement}`; | ||
| /** | ||
| * The required code points. | ||
| */ | ||
| readonly requirement: TRequirement; | ||
| /** | ||
| * The error message. | ||
| */ | ||
| readonly message: TMessage; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a code points validation action. | ||
| * | ||
| * @param requirement The required code points. | ||
| * | ||
| * @returns A code points action. | ||
| */ | ||
| export function codePoints< | ||
| TInput extends string, | ||
| const TRequirement extends number, | ||
| >(requirement: TRequirement): CodePointsAction<TInput, TRequirement, undefined>; | ||
|
|
||
| /** | ||
| * Creates a code points validation action. | ||
| * | ||
| * @param requirement The required code points. | ||
| * @param message The error message. | ||
| * | ||
| * @returns A code points action. | ||
| */ | ||
| export function codePoints< | ||
| TInput extends string, | ||
| const TRequirement extends number, | ||
| const TMessage extends | ||
| | ErrorMessage<CodePointsIssue<TInput, TRequirement>> | ||
| | undefined, | ||
| >( | ||
| requirement: TRequirement, | ||
| message: TMessage | ||
| ): CodePointsAction<TInput, TRequirement, TMessage>; | ||
|
|
||
| // @__NO_SIDE_EFFECTS__ | ||
| export function codePoints( | ||
|
tats-u marked this conversation as resolved.
|
||
| requirement: number, | ||
| message?: ErrorMessage<CodePointsIssue<string, number>> | ||
| ): CodePointsAction< | ||
| string, | ||
| number, | ||
| ErrorMessage<CodePointsIssue<string, number>> | undefined | ||
| > { | ||
| return { | ||
| kind: 'validation', | ||
| type: 'code_points', | ||
| reference: codePoints, | ||
| async: false, | ||
| expects: `${requirement}`, | ||
| requirement, | ||
| message, | ||
| '~run'(dataset, config) { | ||
| if (dataset.typed) { | ||
| const count = _getCodePointCount(dataset.value); | ||
| if (count !== this.requirement) { | ||
| _addIssue(this, 'code_points', dataset, config, { | ||
| received: `${count}`, | ||
| }); | ||
| } | ||
| } | ||
| return dataset; | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or 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 @@ | ||
| export * from './codePoints.ts'; |
This file contains hidden or 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 hidden or 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 @@ | ||
| export * from './maxCodePoints.ts'; |
This file contains hidden or 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,50 @@ | ||
| import { describe, expectTypeOf, test } from 'vitest'; | ||
| import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts'; | ||
| import { | ||
| maxCodePoints, | ||
| type MaxCodePointsAction, | ||
| type MaxCodePointsIssue, | ||
| } from './maxCodePoints.ts'; | ||
|
|
||
| describe('maxCodePoints', () => { | ||
| describe('should return action object', () => { | ||
| test('with undefined message', () => { | ||
| type Action = MaxCodePointsAction<string, 10, undefined>; | ||
| expectTypeOf(maxCodePoints<string, 10>(10)).toEqualTypeOf<Action>(); | ||
| expectTypeOf( | ||
| maxCodePoints<string, 10, undefined>(10, undefined) | ||
| ).toEqualTypeOf<Action>(); | ||
| }); | ||
|
|
||
| test('with string message', () => { | ||
| expectTypeOf( | ||
| maxCodePoints<string, 10, 'message'>(10, 'message') | ||
| ).toEqualTypeOf<MaxCodePointsAction<string, 10, 'message'>>(); | ||
| }); | ||
|
|
||
| test('with function message', () => { | ||
| expectTypeOf( | ||
| maxCodePoints<string, 10, () => string>(10, () => 'message') | ||
| ).toEqualTypeOf<MaxCodePointsAction<string, 10, () => string>>(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('should infer correct types', () => { | ||
| type Input = 'example string'; | ||
| type Action = MaxCodePointsAction<Input, 10, undefined>; | ||
|
|
||
| test('of input', () => { | ||
| expectTypeOf<InferInput<Action>>().toEqualTypeOf<Input>(); | ||
| }); | ||
|
|
||
| test('of output', () => { | ||
| expectTypeOf<InferOutput<Action>>().toEqualTypeOf<Input>(); | ||
| }); | ||
|
|
||
| test('of issue', () => { | ||
| expectTypeOf<InferIssue<Action>>().toEqualTypeOf< | ||
| MaxCodePointsIssue<Input, 10> | ||
| >(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.