11import { StandardSchemaV1 } from '@standard-schema/spec'
22
33const kSchema = Symbol . for ( 'schemastery' )
4- const kValidationError = Symbol . for ( 'ValidationError' )
54
6- export interface ParseOptions {
5+ export interface ValidateOptions {
76 autofix ?: boolean
87 ignore ?: ( data : any , schema : Schema ) => boolean
98 path ?: PropertyKey [ ]
@@ -38,7 +37,7 @@ export abstract class Schema<S = any, T extends S = S> implements StandardSchema
3837 } ,
3938 }
4039
41- abstract validate ( value : unknown , options : ParseOptions ) : Schema . Result < T >
40+ abstract validate ( value : unknown , options : ValidateOptions ) : Schema . Result < T >
4241
4342 format ( ) : string {
4443 return this . type
@@ -61,30 +60,3 @@ export abstract class Schema<S = any, T extends S = S> implements StandardSchema
6160Object . defineProperty ( Schema . prototype , kSchema , {
6261 value : true ,
6362} )
64-
65- export class ValidationError extends TypeError {
66- name = 'ValidationError'
67-
68- constructor ( message : string , public options : ParseOptions ) {
69- let prefix = '$'
70- for ( const segment of options . path || [ ] ) {
71- if ( typeof segment === 'string' ) {
72- prefix += '.' + segment
73- } else if ( typeof segment === 'number' ) {
74- prefix += '[' + segment + ']'
75- } else if ( typeof segment === 'symbol' ) {
76- prefix += `[Symbol(${ segment . toString ( ) } )]`
77- }
78- }
79- if ( prefix . startsWith ( '.' ) ) prefix = prefix . slice ( 1 )
80- super ( ( prefix === '$' ? '' : `${ prefix } ` ) + message )
81- }
82-
83- static is ( error : any ) : error is ValidationError {
84- return ! ! error ?. [ kValidationError ]
85- }
86- }
87-
88- Object . defineProperty ( ValidationError . prototype , kValidationError , {
89- value : true ,
90- } )
0 commit comments