-
Notifications
You must be signed in to change notification settings - Fork 0
/
const-property-schema.ts
37 lines (29 loc) · 1.05 KB
/
const-property-schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { is } from '@mojule/is'
import { assertStringSchema } from './string-schema'
import { ConstPropertySchema } from './types'
export const isConstPropertySchema =
( value ) : value is ConstPropertySchema => {
try {
assertConstPropertySchema( value )
} catch {
return false
}
return true
}
export const assertConstPropertySchema = (
constPropertySchema, name = 'ConstPropertySchema'
) => {
assertStringSchema( constPropertySchema )
if ( !is.array( constPropertySchema.enum ) )
throw TypeError( `${ name }.enum should be an array` )
if ( !is.string( constPropertySchema.enum[ 0 ] ) )
throw TypeError( `${ name }.enum[0] should be a string` )
if ( !is.string( constPropertySchema.default ) )
throw TypeError( `${ name }.default should be a string` )
if ( constPropertySchema.enum[ 0 ] !== constPropertySchema.default )
throw TypeError(
`${ name }.enum[0] should match ${ name }.default`
)
if( constPropertySchema.readOnly !== true )
throw TypeError( `${ name }.readOnly should be true` )
}