-
-
Notifications
You must be signed in to change notification settings - Fork 165
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
feat: implement Standard Schema #1500
Open
sevenc-nanashi
wants to merge
16
commits into
samchon:master
Choose a base branch
from
sevenc-nanashi:feat/standard-schema
base: master
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d15da94
feat: add standard schema as a dependency
sevenc-nanashi 8013982
feat: implement standard schema
sevenc-nanashi daa63f8
feat: add StandardSchema to validateEquals
sevenc-nanashi f04c844
fix: fix typing
sevenc-nanashi f04f717
test: add tests for standard schema
sevenc-nanashi 5a03ca6
fix: fix factories didn't have standard schemas
sevenc-nanashi eaca5bd
fix: fix test
sevenc-nanashi c156d31
fix: fix test failing
sevenc-nanashi 77ba054
fix: fix parser
sevenc-nanashi cb58ab6
fix: fix parser again
sevenc-nanashi c707c5b
chore: remove $input from path
sevenc-nanashi 1bcdb31
test: add tests via generator
sevenc-nanashi d31c75a
fix: fix test compile failing
sevenc-nanashi 563d3a4
fix: fix path parser
sevenc-nanashi 1b86342
fix: fix test of standard validator
sevenc-nanashi fe43c31
fix: fix test failing
sevenc-nanashi 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 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,134 @@ | ||
import { StandardSchemaV1 } from "@standard-schema/spec"; | ||
|
||
import { IValidation } from "../IValidation"; | ||
|
||
export const _createStandardSchema = <T>( | ||
fn: (input: unknown) => IValidation<T>, | ||
) => | ||
Object.assign(fn, { | ||
"~standard": { | ||
version: 1, | ||
vendor: "typia", | ||
validate: (input: unknown): StandardSchemaV1.Result<T> => { | ||
const result = fn(input); | ||
if (result.success) { | ||
return { | ||
value: result.data, | ||
} satisfies StandardSchemaV1.SuccessResult<T>; | ||
} else { | ||
return { | ||
issues: result.errors.map((error) => ({ | ||
message: `expected ${error.expected}, got ${error.value}`, | ||
path: typiaPathToStandardSchemaPath(error.path), | ||
})), | ||
} satisfies StandardSchemaV1.FailureResult; | ||
} | ||
}, | ||
}, | ||
} satisfies StandardSchemaV1<unknown, T>); | ||
|
||
enum PathParserState { | ||
// Start of a new segment | ||
// When the parser is in this state, | ||
// the pointer must point `.` or `[` or equal to length of the path | ||
Start, | ||
// Parsing a property key (`.hoge`) | ||
Property, | ||
// Parsing a string key (`["fuga"]`) | ||
StringKey, | ||
// Parsing a number key (`[42]`) | ||
NumberKey, | ||
} | ||
|
||
const typiaPathToStandardSchemaPath = ( | ||
path: string, | ||
): ReadonlyArray<StandardSchemaV1.PathSegment> => { | ||
if (!path.startsWith("$input")) { | ||
throw new Error(`Invalid path: ${JSON.stringify(path)}`); | ||
} | ||
|
||
const segments: StandardSchemaV1.PathSegment[] = []; | ||
let currentSegment = ""; | ||
let state: PathParserState = PathParserState.Start; | ||
let index = "$input".length - 1; | ||
while (index < path.length - 1) { | ||
index++; | ||
const char = path[index]; | ||
|
||
if (state === PathParserState.Property) { | ||
if (char === "." || char === "[") { | ||
// End of property | ||
segments.push({ | ||
key: currentSegment, | ||
}); | ||
state = PathParserState.Start; | ||
} else if (index === path.length - 1) { | ||
// End of path | ||
currentSegment += char; | ||
segments.push({ | ||
key: currentSegment, | ||
}); | ||
index++; | ||
state = PathParserState.Start; | ||
} else { | ||
currentSegment += char; | ||
} | ||
} else if (state === PathParserState.StringKey) { | ||
if (char === '"') { | ||
// End of string key | ||
segments.push({ | ||
key: JSON.parse(currentSegment + char), | ||
}); | ||
// Skip `"` and `]` | ||
index += 2; | ||
state = PathParserState.Start; | ||
} else if (char === "\\") { | ||
// Skip the next character from parsing | ||
currentSegment += path[index]; | ||
index++; | ||
currentSegment += path[index]; | ||
} else { | ||
currentSegment += char; | ||
} | ||
} else if (state === PathParserState.NumberKey) { | ||
if (char === "]") { | ||
// End of number key | ||
segments.push({ | ||
key: Number.parseInt(currentSegment), | ||
}); | ||
index++; | ||
state = PathParserState.Start; | ||
} else { | ||
currentSegment += char; | ||
} | ||
} | ||
|
||
if (state === PathParserState.Start && index < path.length - 1) { | ||
const newChar = path[index]; | ||
currentSegment = ""; | ||
if (newChar === "[") { | ||
if (path[index + 1] === '"') { | ||
// Start of string key | ||
// NOTE: Typia uses JSON.stringify for this kind of keys, so `'` will not used as a string delimiter | ||
state = PathParserState.StringKey; | ||
index++; | ||
currentSegment = '"'; | ||
} else { | ||
// Start of number key | ||
state = PathParserState.NumberKey; | ||
} | ||
} else if (newChar === ".") { | ||
// Start of property | ||
state = PathParserState.Property; | ||
} else { | ||
throw new Error("Unreachable: pointer points invalid character"); | ||
} | ||
} | ||
} | ||
|
||
if (state !== PathParserState.Start) { | ||
throw new Error(`Failed to parse path: ${JSON.stringify(path)}`); | ||
} | ||
|
||
return segments; | ||
}; |
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 |
---|---|---|
|
@@ -20,6 +20,7 @@ import { check_object } from "./internal/check_object"; | |
export namespace ValidateProgrammer { | ||
export interface IConfig { | ||
equals: boolean; | ||
standardSchema?: boolean; | ||
} | ||
export interface IProps extends IProgrammerProps { | ||
config: IConfig; | ||
|
@@ -241,6 +242,14 @@ export namespace ValidateProgrammer { | |
modulo: props.modulo, | ||
functor, | ||
result, | ||
returnWrapper: props.config.standardSchema | ||
? (arrow) => | ||
ts.factory.createCallExpression( | ||
props.context.importer.internal("createStandardSchema"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if it's okay to use this method here... |
||
undefined, | ||
[arrow], | ||
) | ||
: undefined, | ||
}); | ||
}; | ||
} | ||
|
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
10 changes: 10 additions & 0 deletions
10
...src/features/standardSchema.createValidate/test_standardSchema_createValidate_ArrayAny.ts
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,10 @@ | ||
import typia from "typia"; | ||
|
||
import { _test_standardSchema_validate } from "../../internal/_test_standardSchema_validate"; | ||
import { ArrayAny } from "../../structures/ArrayAny"; | ||
|
||
export const test_standardSchema_createValidate_ArrayAny = _test_standardSchema_validate( | ||
"ArrayAny", | ||
)<ArrayAny>( | ||
ArrayAny | ||
)(typia.createValidate<ArrayAny>()); |
10 changes: 10 additions & 0 deletions
10
...ures/standardSchema.createValidate/test_standardSchema_createValidate_ArrayAtomicAlias.ts
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,10 @@ | ||
import typia from "typia"; | ||
|
||
import { _test_standardSchema_validate } from "../../internal/_test_standardSchema_validate"; | ||
import { ArrayAtomicAlias } from "../../structures/ArrayAtomicAlias"; | ||
|
||
export const test_standardSchema_createValidate_ArrayAtomicAlias = _test_standardSchema_validate( | ||
"ArrayAtomicAlias", | ||
)<ArrayAtomicAlias>( | ||
ArrayAtomicAlias | ||
)(typia.createValidate<ArrayAtomicAlias>()); |
10 changes: 10 additions & 0 deletions
10
...res/standardSchema.createValidate/test_standardSchema_createValidate_ArrayAtomicSimple.ts
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,10 @@ | ||
import typia from "typia"; | ||
|
||
import { _test_standardSchema_validate } from "../../internal/_test_standardSchema_validate"; | ||
import { ArrayAtomicSimple } from "../../structures/ArrayAtomicSimple"; | ||
|
||
export const test_standardSchema_createValidate_ArrayAtomicSimple = _test_standardSchema_validate( | ||
"ArrayAtomicSimple", | ||
)<ArrayAtomicSimple>( | ||
ArrayAtomicSimple | ||
)(typia.createValidate<ArrayAtomicSimple>()); |
10 changes: 10 additions & 0 deletions
10
...res/standardSchema.createValidate/test_standardSchema_createValidate_ArrayHierarchical.ts
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,10 @@ | ||
import typia from "typia"; | ||
|
||
import { _test_standardSchema_validate } from "../../internal/_test_standardSchema_validate"; | ||
import { ArrayHierarchical } from "../../structures/ArrayHierarchical"; | ||
|
||
export const test_standardSchema_createValidate_ArrayHierarchical = _test_standardSchema_validate( | ||
"ArrayHierarchical", | ||
)<ArrayHierarchical>( | ||
ArrayHierarchical | ||
)(typia.createValidate<ArrayHierarchical>()); |
10 changes: 10 additions & 0 deletions
10
...ndardSchema.createValidate/test_standardSchema_createValidate_ArrayHierarchicalPointer.ts
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,10 @@ | ||
import typia from "typia"; | ||
|
||
import { _test_standardSchema_validate } from "../../internal/_test_standardSchema_validate"; | ||
import { ArrayHierarchicalPointer } from "../../structures/ArrayHierarchicalPointer"; | ||
|
||
export const test_standardSchema_createValidate_ArrayHierarchicalPointer = _test_standardSchema_validate( | ||
"ArrayHierarchicalPointer", | ||
)<ArrayHierarchicalPointer>( | ||
ArrayHierarchicalPointer | ||
)(typia.createValidate<ArrayHierarchicalPointer>()); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looked so difficult and require large changes to add
_pathArray
parameter, so I created a parser to split intoSegmentPath[]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also where should I write the unit test for this function?nvm, I found by myself