@@ -28,8 +28,6 @@ THE SOFTWARE.
2828
2929// deno-fmt-ignore-file
3030
31- import { type TLocalizedValidationError , type TStandardSchemaV1Error , type TValidationErrorBase , IsLocalizedValidationError } from '../../error/index.ts'
32- import { Guard } from '../../guard/index.ts'
3331import { type TSchema , IsKind } from './schema.ts'
3432
3533// ------------------------------------------------------------------
@@ -47,32 +45,8 @@ export class BaseNotImplemented extends Error {
4745 } )
4846 }
4947}
50- // ------------------------------------------------------------------------------------
51- // BaseValidator
52- // ------------------------------------------------------------------------------------
53- class BaseValidator < Value extends unknown = unknown > implements StandardSchemaV1 . Props < Value > {
54- public readonly vendor = 'typebox'
55- public readonly version = 1
56- constructor (
57- private readonly check : ( value : unknown ) => boolean ,
58- private readonly errors : ( value : unknown ) => object [ ]
59- ) { }
60- public readonly validate = ( value : unknown ) : StandardSchemaV1 . Result < Value > => {
61- return this . check ( value )
62- ? this . Success ( value as Value )
63- : this . Failure ( this . errors ( value ) )
64- }
65- private Success ( value : Value ) : StandardSchemaV1 . SuccessResult < Value > {
66- return { value }
67- }
68- private Failure ( errors : object [ ] ) : StandardSchemaV1 . FailureResult {
69- const issues = errors . reduce < StandardSchemaV1 . Issue [ ] > ( ( result , error ) =>
70- [ ...result , ...CreateIssues ( error ) ] , [ ] )
71- return { issues } as never
72- }
73- }
7448// ------------------------------------------------------------------
75- // Type
49+ // Type.Base<...>
7650// ------------------------------------------------------------------
7751/** Base class for creating extension types. */
7852export class Base < Value extends unknown = unknown > implements TSchema {
@@ -123,58 +97,30 @@ export class Base<Value extends unknown = unknown> implements TSchema {
12397export function IsBase ( value : unknown ) : value is Base {
12498 return IsKind ( value , 'Base' )
12599}
126- // --------------------------------------------------------
127- // StandardSchema: PathSegments
128- // --------------------------------------------------------
129- function PathSegments ( pointer : string ) : string [ ] {
130- if ( Guard . IsEqual ( pointer . length , 0 ) ) return [ ]
131- return pointer . slice ( 1 ) . split ( "/" ) . map ( segment => segment . replace ( / ~ 1 / g, "/" ) . replace ( / ~ 0 / g, "~" ) )
132- }
133- // --------------------------------------------------------
134- // IsStandardSchemaV1Error
135- // --------------------------------------------------------
136- function IsStandardSchemaV1Error ( error : TValidationErrorBase ) : error is TStandardSchemaV1Error {
137- return Guard . IsEqual ( error . keyword , '~standard' )
138- }
139- // --------------------------------------------------------
140- // IssuesFromLocalizedError
141- // --------------------------------------------------------
142- function IssuesFromStandardSchemaV1Error ( error : TStandardSchemaV1Error ) : StandardSchemaV1 . Issue [ ] {
143- const leading = PathSegments ( error . instancePath )
144- const issues = Guard . IsArray ( error . params . issues ) ? error . params . issues : [ ]
145- return issues . map ( issue => {
146- const message = Guard . IsString ( issue . message ) ? issue . message : 'unknown'
147- const path = Guard . IsArray ( issue . path ) ? [ ...leading , ...issue . path ] : leading
148- return { message, path }
149- } )
150- }
151- function IssuesFromRegularError ( error : TLocalizedValidationError ) : StandardSchemaV1 . Issue [ ] {
152- const path = PathSegments ( error . instancePath )
153- return [ { path, message : error . message } ]
154- }
155- function IssuesFromLocalizedError ( error : TLocalizedValidationError ) : StandardSchemaV1 . Issue [ ] {
156- return IsStandardSchemaV1Error ( error )
157- ? IssuesFromStandardSchemaV1Error ( error )
158- : IssuesFromRegularError ( error )
159- }
160- // --------------------------------------------------------
161- // IssuesFromUnknown
162- // --------------------------------------------------------
163- function IssuesFromUnknown ( error : object ) : StandardSchemaV1 . Issue [ ] {
164- const path = Guard . HasPropertyKey ( error , 'path' ) && Guard . IsArray ( error . path ) && error . path . every ( segment => Guard . IsString ( segment ) ) ? error . path : [ ]
165- const message = Guard . HasPropertyKey ( error , 'message' ) && Guard . IsString ( error . message ) ? error . message : 'unknown'
166- return [ { path, message } as StandardSchemaV1 . Issue ]
167- }
168- // --------------------------------------------------------
169- // CreateIssues
170- // --------------------------------------------------------
171- function CreateIssues ( error : object ) : StandardSchemaV1 . Issue [ ] {
172- return IsLocalizedValidationError ( error )
173- ? IssuesFromLocalizedError ( error )
174- : IssuesFromUnknown ( error )
100+ // ------------------------------------------------------------------
101+ // BaseValidator
102+ // ------------------------------------------------------------------
103+ class BaseValidator < Value extends unknown = unknown > implements StandardSchemaV1 . Props < Value > {
104+ public readonly vendor = 'typebox'
105+ public readonly version = 1
106+ constructor (
107+ private readonly check : ( value : unknown ) => value is Value ,
108+ private readonly errors : ( value : unknown ) => object [ ]
109+ ) { }
110+ public readonly validate = ( value : unknown ) : StandardSchemaV1 . Result < Value > => {
111+ return this . check ( value )
112+ ? this . Success ( value )
113+ : this . Failure ( this . errors ( value ) )
114+ }
115+ private Success ( value : Value ) : StandardSchemaV1 . SuccessResult < Value > {
116+ return { value }
117+ }
118+ private Failure ( issues : object [ ] ) : StandardSchemaV1 . FailureResult {
119+ return { issues } as never
120+ }
175121}
176122// ------------------------------------------------------------------
177- // Standard Schema Interface
123+ // The Standard? Schema?? V1 Interface
178124// ------------------------------------------------------------------
179125interface StandardSchemaV1 < Input = unknown , Output = Input > {
180126 readonly '~standard' : StandardSchemaV1 . Props < Input , Output >
0 commit comments