11import { getTypeOf } from "./getTypeOf" ;
22import { isObject } from "../utils/isObject" ;
3- import { SchemaNode } from "../types" ;
3+ import { BooleanSchema , JsonSchema , SchemaNode } from "../types" ;
44
55export const SCHEMA_TYPES = [ "string" , "number" , "integer" , "boolean" , "null" , "array" , "object" ] ;
66const OBJECT_PROPERTIES = [
@@ -45,22 +45,22 @@ const ARRAY_PROPERTIES = [
4545 */
4646export function getSchemaType ( node : SchemaNode , data : unknown ) : keyof typeof SCHEMA_TYPES | undefined {
4747 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 ) {
5050 return SCHEMA_TYPES . includes ( dataType ) ? ( dataType as keyof typeof SCHEMA_TYPES ) : undefined ;
5151 }
5252 // boolean schema false or invalid schema
53- if ( ! isObject ( node . schema ) ) {
53+ if ( ! isObject ( schema ) ) {
5454 return undefined ;
5555 }
56- const schemaType = node . schema . type ;
56+ const schemaType = schema . type ;
5757
5858 // type: []
5959 if ( Array . isArray ( schemaType ) ) {
6060 if ( schemaType . includes ( dataType ) ) {
6161 return dataType as keyof typeof SCHEMA_TYPES ;
6262 }
63- const defaultType = getTypeOf ( node . schema . default ) ;
63+ const defaultType = getTypeOf ( schema . default ) ;
6464 if ( schemaType . includes ( defaultType ) ) {
6565 return defaultType as keyof typeof SCHEMA_TYPES ;
6666 }
@@ -73,13 +73,13 @@ export function getSchemaType(node: SchemaNode, data: unknown): keyof typeof SCH
7373 }
7474
7575 // 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 ;
7878 const enumSchemaType = schemaEnum . map ( ( value ) => getTypeOf ( value ) ) . filter ( ( p , i , l ) => l . indexOf ( p ) === i ) ;
7979 if ( enumSchemaType . includes ( dataType ) ) {
8080 return dataType as keyof typeof SCHEMA_TYPES ;
8181 }
82- const defaultType = getTypeOf ( node . schema . default ) ;
82+ const defaultType = getTypeOf ( schema . default ) ;
8383 if ( enumSchemaType . includes ( defaultType ) ) {
8484 return defaultType as keyof typeof SCHEMA_TYPES ;
8585 }
0 commit comments