grantila / suretype

Typesafe JSON (Schema) validator
502 stars 9 forks source link

How to define object key & values? #11

Closed crobinson42 closed 3 years ago

crobinson42 commented 3 years ago

I can't seem to find a solution for typing object's key=>value types, ie:

type Item = {
 [key: string]: StoreItem
}

Something like this would be a nice addition to the API:

import { v } from 'suretype'

const Item = v.objectKeyValue( v.string(), v.object({ ... }) )
grantila commented 3 years ago

@crobinson42 try an empty object (i.e. with no specific property defined) and use additional. The key being a string is implied.

v.object( )
    .additional( v.object( { ... } ) );

Please re-open if this didn't do it!

crobinson42 commented 2 years ago

@grantila please re-open this issue - this error happens with your suggestion: TypeError: Cannot convert undefined or null to object

crobinson42 commented 2 years ago

Disregard - it works with an empty object:

v.object( {} )
    .additional( v.object( { ... } ) );