1
1
import { getTypeOf } from "./getTypeOf" ;
2
2
import { isObject } from "../utils/isObject" ;
3
- import { SchemaNode } from "../types" ;
3
+ import { BooleanSchema , JsonSchema , SchemaNode } from "../types" ;
4
4
5
5
export const SCHEMA_TYPES = [ "string" , "number" , "integer" , "boolean" , "null" , "array" , "object" ] ;
6
6
const OBJECT_PROPERTIES = [
@@ -45,22 +45,22 @@ const ARRAY_PROPERTIES = [
45
45
*/
46
46
export function getSchemaType ( node : SchemaNode , data : unknown ) : keyof typeof SCHEMA_TYPES | undefined {
47
47
const dataType = getTypeOf ( data ) ;
48
- // @ts -expect-error boolean schema true
49
- if ( node . schema === true ) {
48
+ const schema = node . schema as JsonSchema | BooleanSchema ;
49
+ if ( schema === true ) {
50
50
return SCHEMA_TYPES . includes ( dataType ) ? ( dataType as keyof typeof SCHEMA_TYPES ) : undefined ;
51
51
}
52
52
// boolean schema false or invalid schema
53
- if ( ! isObject ( node . schema ) ) {
53
+ if ( ! isObject ( schema ) ) {
54
54
return undefined ;
55
55
}
56
- const schemaType = node . schema . type ;
56
+ const schemaType = schema . type ;
57
57
58
58
// type: []
59
59
if ( Array . isArray ( schemaType ) ) {
60
60
if ( schemaType . includes ( dataType ) ) {
61
61
return dataType as keyof typeof SCHEMA_TYPES ;
62
62
}
63
- const defaultType = getTypeOf ( node . schema . default ) ;
63
+ const defaultType = getTypeOf ( schema . default ) ;
64
64
if ( schemaType . includes ( defaultType ) ) {
65
65
return defaultType as keyof typeof SCHEMA_TYPES ;
66
66
}
@@ -73,13 +73,13 @@ export function getSchemaType(node: SchemaNode, data: unknown): keyof typeof SCH
73
73
}
74
74
75
75
// type: undefined, enum: []
76
- if ( Array . isArray ( node . schema . enum ) ) {
77
- const schemaEnum : unknown [ ] = node . schema . enum ;
76
+ if ( Array . isArray ( schema . enum ) ) {
77
+ const schemaEnum : unknown [ ] = schema . enum ;
78
78
const enumSchemaType = schemaEnum . map ( ( value ) => getTypeOf ( value ) ) . filter ( ( p , i , l ) => l . indexOf ( p ) === i ) ;
79
79
if ( enumSchemaType . includes ( dataType ) ) {
80
80
return dataType as keyof typeof SCHEMA_TYPES ;
81
81
}
82
- const defaultType = getTypeOf ( node . schema . default ) ;
82
+ const defaultType = getTypeOf ( schema . default ) ;
83
83
if ( enumSchemaType . includes ( defaultType ) ) {
84
84
return defaultType as keyof typeof SCHEMA_TYPES ;
85
85
}
0 commit comments