@@ -785,56 +785,26 @@ function buildSingleTypeSerializer (location, input) {
785
785
786
786
function buildConstSerializer ( location , input ) {
787
787
const schema = location . schema
788
- let schemaRef = location . getSchemaRef ( )
789
- if ( schemaRef . startsWith ( rootSchemaId ) ) {
790
- schemaRef = schemaRef . replace ( rootSchemaId , ' ')
791
- }
788
+ const type = schema . type
789
+
790
+ const hasNullType = Array . isArray ( type ) && type . includes ( 'null ')
791
+
792
792
let code = ''
793
793
794
- switch ( typeof schema . const ) {
795
- case 'bigint' :
796
- code += `
797
- if (${ input } === ${ schema . const } n) {
798
- json += '${ Number ( schema . const ) } '
799
- } else {
800
- throw new Error(\`The value of '${ schemaRef } ' does not match schema definition.\`)
801
- }`
802
- break
803
- case 'number' :
804
- case 'boolean' :
805
- code += `
806
- if (${ input } === ${ schema . const } ) {
807
- json += ${ JSON . stringify ( JSON . stringify ( schema . const ) ) }
808
- } else {
809
- throw new Error(\`The value of '${ schemaRef } ' does not match schema definition.\`)
810
- }`
811
- break
812
- case 'object' :
813
- if ( schema . const === null ) {
814
- code += `
815
- if (${ input } === null) {
816
- json += 'null'
817
- } else {
818
- throw new Error(\`The value of '${ schemaRef } ' does not match schema definition.\`)
819
- }`
794
+ if ( hasNullType ) {
795
+ code += `
796
+ if (${ input } === null) {
797
+ json += 'null'
820
798
} else {
821
- code += `
822
- if (JSON.stringify(${ input } ) === '${ JSON . stringify ( schema . const ) } ') {
823
- json += ${ JSON . stringify ( JSON . stringify ( schema . const ) ) }
824
- } else {
825
- throw new Error(\`The value of '${ schemaRef } ' does not match schema definition.\`)
826
- }`
799
+ `
800
+ }
801
+
802
+ code += `json += '${ JSON . stringify ( schema . const ) } '`
803
+
804
+ if ( hasNullType ) {
805
+ code += `
827
806
}
828
- break
829
- case 'string' :
830
- case 'undefined' :
831
- code += `
832
- if (${ input } === '${ schema . const } ') {
833
- json += ${ JSON . stringify ( JSON . stringify ( schema . const ) ) }
834
- } else {
835
- throw new Error(\`The value of '${ schemaRef } ' does not match schema definition.\`)
836
- }`
837
- break
807
+ `
838
808
}
839
809
840
810
return code
@@ -899,7 +869,7 @@ function buildValue (location, input) {
899
869
return code
900
870
}
901
871
902
- const nullable = schema . nullable === true && ! ( 'const' in schema )
872
+ const nullable = schema . nullable === true
903
873
if ( nullable ) {
904
874
code += `
905
875
if (${ input } === null) {
0 commit comments