1+ /*--------------------------------------------------------------------------
2+
3+ TypeBox
4+
5+ The MIT License (MIT)
6+
7+ Copyright (c) 2017-2025 Haydn Paterson
8+
9+ Permission is hereby granted, free of charge, to any person obtaining a copy
10+ of this software and associated documentation files (the "Software"), to deal
11+ in the Software without restriction, including without limitation the rights
12+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+ copies of the Software, and to permit persons to whom the Software is
14+ furnished to do so, subject to the following conditions:
15+
16+ The above copyright notice and this permission notice shall be included in
17+ all copies or substantial portions of the Software.
18+
19+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+ THE SOFTWARE.
26+
27+ ---------------------------------------------------------------------------*/
28+
29+ import { type TLocalizedValidationError , type TValidationErrorBase , type TStandardSchemaV1Error , IsLocalizedValidationError } from 'typebox/error'
30+ import { StandardSchemaV1 } from './standard-schema-v1.ts'
31+ import Guard from 'typebox/guard'
32+
33+ // --------------------------------------------------------
34+ // StandardSchema: PathSegments
35+ // --------------------------------------------------------
36+ function PathSegments ( pointer : string ) : string [ ] {
37+ if ( Guard . IsEqual ( pointer . length , 0 ) ) return [ ]
38+ return pointer . slice ( 1 ) . split ( "/" ) . map ( segment => segment . replace ( / ~ 1 / g, "/" ) . replace ( / ~ 0 / g, "~" ) )
39+ }
40+ // --------------------------------------------------------
41+ // IsStandardSchemaV1Error
42+ // --------------------------------------------------------
43+ function IsStandardSchemaV1Error ( error : TValidationErrorBase ) : error is TStandardSchemaV1Error {
44+ return Guard . IsEqual ( error . keyword , '~standard' )
45+ }
46+ // --------------------------------------------------------
47+ // IssuesFromLocalizedError
48+ // --------------------------------------------------------
49+ function IssuesFromStandardSchemaV1Error ( error : TStandardSchemaV1Error ) : StandardSchemaV1 . Issue [ ] {
50+ const leading = PathSegments ( error . instancePath )
51+ const issues = Guard . IsArray ( error . params . issues ) ? error . params . issues : [ ]
52+ return issues . map ( issue => {
53+ const message = Guard . IsString ( issue . message ) ? issue . message : 'unknown'
54+ const path = Guard . IsArray ( issue . path ) ? [ ...leading , ...issue . path ] : leading
55+ return { message, path }
56+ } )
57+ }
58+ function IssuesFromRegularError ( error : TLocalizedValidationError ) : StandardSchemaV1 . Issue [ ] {
59+ const path = PathSegments ( error . instancePath )
60+ return [ { path, message : error . message } ]
61+ }
62+ function IssuesFromLocalizedError ( error : TLocalizedValidationError ) : StandardSchemaV1 . Issue [ ] {
63+ return IsStandardSchemaV1Error ( error )
64+ ? IssuesFromStandardSchemaV1Error ( error )
65+ : IssuesFromRegularError ( error )
66+ }
67+ // --------------------------------------------------------
68+ // IssuesFromUnknown
69+ // --------------------------------------------------------
70+ function IssuesFromUnknown ( error : object ) : StandardSchemaV1 . Issue [ ] {
71+ const path = Guard . HasPropertyKey ( error , 'path' ) && Guard . IsArray ( error . path ) && error . path . every ( segment => Guard . IsString ( segment ) ) ? error . path : [ ]
72+ const message = Guard . HasPropertyKey ( error , 'message' ) && Guard . IsString ( error . message ) ? error . message : 'unknown'
73+ return [ { path, message } as StandardSchemaV1 . Issue ]
74+ }
75+ // --------------------------------------------------------
76+ // CreateIssues
77+ // --------------------------------------------------------
78+ function FromError ( error : object ) : StandardSchemaV1 . Issue [ ] {
79+ return IsLocalizedValidationError ( error )
80+ ? IssuesFromLocalizedError ( error )
81+ : IssuesFromUnknown ( error )
82+ }
83+ // --------------------------------------------------------
84+ // ToStandardSchemaV1Issues
85+ // --------------------------------------------------------
86+ export type TErrorLike = TValidationErrorBase | StandardSchemaV1 . Issue
87+
88+ /** Transforms an array of ErrorLikes into a consistent StandardSchemaV1.Issue[] array. */
89+ export function ToStandardSchemaV1Issues ( errorLikes : TErrorLike [ ] ) : StandardSchemaV1 . Issue [ ] {
90+ return errorLikes . reduce < StandardSchemaV1 . Issue [ ] > ( ( result , error ) => {
91+ return [ ...result , ...FromError ( error ) ]
92+ } , [ ] )
93+ }
0 commit comments