gristlabs/ts-interface-checker

Unknown type Date

Brijak0 opened this issue · 4 comments

Hi,
I'm having an issue when creating a checker using your ts-interface-checker tool.
I do the following:

private validateEventData(){
        const {eventData} = createCheckers(EventDataTI);
        try{
            eventData.check({domain: this.EventData.domain});
            eventData.check({userId: this.EventData.userId});                          
            eventData.check({actorObjectType: this.EventData.actorObjectType});
            eventData.check({modified: this.EventData.modified});
            eventData.check({registrationId: this.EventData.registrationId});
       }
        catch(ex){
             let validationError = new ValidationError(ex);  
             throw(validationError); 
        }
    }

When I try to create the checker I get the following exception

c:\source\AN393\node_modules\ts-interface-checker\dist\types.js:19
        throw new Error(`Unknown type ${name}`);
        ^

Error: Unknown type Date
    at getNamedType (c:\source\AN393\node_modules\ts-interface-checker\dist\types.js:19:15)
    at TName.getChecker (c:\source\AN393\node_modules\ts-interface-checker\dist\types.js:36:23)
    at propCheckers.props.map (c:\source\AN393\node_modules\ts-interface-checker\dist\types.js:194:66)
    at Array.map (<anonymous>)
    at TIface.getChecker (c:\source\AN393\node_modules\ts-interface-checker\dist\types.js:194:41)
    at new Checker (c:\source\AN393\node_modules\ts-interface-checker\dist\index.js:64:40)
    at Object.createCheckers (c:\source\AN393\node_modules\ts-interface-checker\dist\index.js:43:30)
    at XApiFactory.validateEventData (c:\source\AN393\lib\XApiFactory.js:103:48)
    at XApiFactory.createLaunchEvent (c:\source\AN393\lib\XApiFactory.js:29:22)
    at Object.<anonymous> (c:\source\AN393\lib\test\Debug_Test.js:40:23)

Created validation export:

export const IEventData = t.iface([], {
  "domain": "string",
  "userId": "string",
  "actorObjectType": "ActorObjectType",
  "modified": "Date",
  "registrationId": "string",
  "activities": t.array("IEventActivityData"),
  "attachments": t.array("IEventAttachmentData"),
});

I'm trying to validate an object, which contains a Date type it seems like createChecker does not support the Date type?
Is there a workaround this issue?

br and thanks in advance

You are right, Date is not currently supported. You can add support for it as a user as follows:

import {createCheckers, ICheckerSuite} from 'ts-interface-checker';
import {basicTypes, BasicType} from 'ts-interface-checker/dist/types';
basicTypes["Date"] = new BasicType((v) => (v instanceof Date), "is not a Date");

import typeSuite from './foo-ti';
export const checkers = createCheckers(typeSuite);

function validateEventData(data: any) {
  checkers.IEventData.check(data);
}

Thanks for your quick reply. I implemented your described workaround and it work real nicely. :)
Best regards
Brijak0

Excellent :)

Hey - I've just run into this issue, and the work around works fine. Can I suggest supporting this out of the box, or atleast give it the option to?