Skip to content
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
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"homepage": "https://typia.io",
"dependencies": {
"@samchon/openapi": "^2.4.2",
"@standard-schema/spec": "^1.0.0",
"commander": "^10.0.0",
"comment-json": "^4.2.3",
"inquirer": "^8.2.5",
Expand Down
134 changes: 134 additions & 0 deletions src/internal/_createStandardSchema.ts
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 = (
Copy link
Author

@sevenc-nanashi sevenc-nanashi Feb 12, 2025

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 into SegmentPath[].

Copy link
Author

@sevenc-nanashi sevenc-nanashi Feb 12, 2025

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

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;
};
16 changes: 12 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { StandardSchemaV1 } from "@standard-schema/spec";

import { AssertionGuard } from "./AssertionGuard";
import { IRandomGenerator } from "./IRandomGenerator";
import { IValidation } from "./IValidation";
Expand Down Expand Up @@ -730,12 +732,14 @@ export function createValidate(): never;
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createValidate<T>(): (input: unknown) => IValidation<T>;
export function createValidate<T>(): ((input: unknown) => IValidation<T>) &
StandardSchemaV1<unknown, T>;

/**
* @internal
*/
export function createValidate(): (input: unknown) => IValidation {
export function createValidate(): ((input: unknown) => IValidation) &
StandardSchemaV1<unknown, unknown> {
halt("createValidate");
}

Expand Down Expand Up @@ -886,12 +890,16 @@ export function createValidateEquals(): never;
*
* @author Jeongho Nam - https://github.com/samchon
*/
export function createValidateEquals<T>(): (input: unknown) => IValidation<T>;
export function createValidateEquals<T>(): ((
input: unknown,
) => IValidation<T>) &
StandardSchemaV1<unknown, T>;

/**
* @internal
*/
export function createValidateEquals(): (input: unknown) => IValidation {
export function createValidateEquals(): ((input: unknown) => IValidation) &
StandardSchemaV1<unknown, unknown> {
halt("createValidateEquals");
}

Expand Down
7 changes: 6 additions & 1 deletion src/programmers/FeatureProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ export namespace FeatureProgrammer {
modulo: ts.LeftHandSideExpression;
functor: FunctionProgrammer;
result: IDecomposed;
returnWrapper?: (arrow: ts.ArrowFunction) => ts.Expression;
}): ts.CallExpression =>
ts.factory.createCallExpression(
ts.factory.createArrowFunction(
Expand All @@ -311,7 +312,11 @@ export namespace FeatureProgrammer {
.filter(([k]) => props.functor.hasLocal(k))
.map(([_k, v]) => v),
...props.result.statements,
ts.factory.createReturnStatement(props.result.arrow),
ts.factory.createReturnStatement(
props.returnWrapper
? props.returnWrapper(props.result.arrow)
: props.result.arrow,
),
]),
),
undefined,
Expand Down
9 changes: 9 additions & 0 deletions src/programmers/ValidateProgrammer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Copy link
Author

Choose a reason for hiding this comment

The 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,
});
};
}
Expand Down
10 changes: 8 additions & 2 deletions src/transformers/CallExpressionTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,20 @@ const FUNCTORS: Record<string, Record<string, () => Task>> = {
CreateAssertTransformer.transform({ equals: false, guard: false }),
createIs: () => CreateIsTransformer.transform({ equals: false }),
createValidate: () =>
CreateValidateTransformer.transform({ equals: false }),
CreateValidateTransformer.transform({
equals: false,
standardSchema: true,
}),
createAssertEquals: () =>
CreateAssertTransformer.transform({ equals: true, guard: false }),
createAssertGuardEquals: () =>
CreateAssertTransformer.transform({ equals: true, guard: true }),
createEquals: () => CreateIsTransformer.transform({ equals: true }),
createValidateEquals: () =>
CreateValidateTransformer.transform({ equals: true }),
CreateValidateTransformer.transform({
equals: true,
standardSchema: true,
}),
createRandom: () => CreateRandomTransformer.transform,
},
functional: {
Expand Down
10 changes: 10 additions & 0 deletions test/build/internal/TestFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { write_random } from "../writers/write_random";

export interface TestFeature {
module: string | null;
prefix?: string;
method: string;
creatable: boolean;
createOnly?: boolean;
spoilable: boolean;
formData?: boolean;
custom?: true;
Expand Down Expand Up @@ -53,6 +55,14 @@ export namespace TestFeature {
creatable: true,
spoilable: true,
},
{
module: null,
prefix: "standardSchema",
method: "validate",
creatable: false,
createOnly: true,
spoilable: true,
},

// STRICT VALIDATORS
{
Expand Down
17 changes: 13 additions & 4 deletions test/build/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ async function generate(
"src",
"features",
[
feat.module ? `${feat.module}.${method}` : method,
...(feat.custom === true ? ["Custom"] : ""),
feat.prefix ? `${feat.prefix}.` : "",
feat.module ? `${feat.module}.` : "",
method,
feat.custom === true ? "Custom" : "",
].join(""),
].join("/");

Expand All @@ -73,6 +75,8 @@ async function generate(
else if (feat.dynamic === false && s.name.startsWith("Dynamic")) continue;

const location: string = `${path}/test_${
feat.prefix ? `${feat.prefix}_` : ""
}${
feat.module ? `${feat.module}_` : ""
}${method}${feat.custom === true ? "Custom" : ""}_${s.name}.ts`;
await fs.promises.writeFile(
Expand All @@ -93,6 +97,7 @@ function script(
? feat.programmer(create)(struct.name)
: write_common({
module: feat.module,
prefix: feat.prefix,
method,
})(create)(struct.name);
if (false === method.toLowerCase().includes("assert")) return content;
Expand Down Expand Up @@ -155,8 +160,12 @@ async function main(): Promise<void> {
})),
];
for (const feature of featureList) {
await generate(feature, structures, false);
if (feature.creatable) await generate(feature, structures, true);
if (feature.createOnly) {
await generate(feature, structures, true);
} else {
await generate(feature, structures, false);
if (feature.creatable) await generate(feature, structures, true);
}
}

// SCHEMAS
Expand Down
4 changes: 3 additions & 1 deletion test/build/writers/write_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export const ${file(p)}_${structure} = _${file({
)(${functor(p)(create)(structure)});
`;

const file = (p: IProps) => "test_" + method(p).replace(".", "_");
const file = (p: IProps) =>
"test_" + (p.prefix ? `${p.prefix}_` : "") + method(p).replace(".", "_");
const method = (p: IProps) =>
[p.module, p.method].filter((str) => !!str).join(".");
const functor = (p: IProps) => (create: boolean) => (structure: string) =>
Expand All @@ -39,5 +40,6 @@ const functor = (p: IProps) => (create: boolean) => (structure: string) =>

interface IProps {
module: string | null;
prefix?: string;
method: string;
}
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>());
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>());
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>());
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>());
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>());
Loading
Loading