angular - Including json schema files in typescript -
i writing angular 2 app typescript using angular-cli tool. have bunch of json schemas back-end api want reference in typescript can pass schemas schema validator. best way include .json files can reference them variable in typescript , have them bundled when build angular-cli?
my first thought make custom module , export each schema const. not sure if that's possible , have been unable find example syntax on how reference files.
here example code works now. problem had copy schema file contents typesrcipt. want able reference original schema file.
import { injectable } "@angular/core"; import { http, response } "@angular/http"; import { observable } "rxjs/observable"; import { wlanstatus } "./wlan-status"; import { jsonvalidatorservice } "../../json-validator.service"; //import wlanstatusschema "../../../api/app/plugins/wifi/schemas/getstatus.json"; const wlanstatusschema = { "type": "object", "properties": { "result": { "type": "object", "properties": { "cardstate": { "type": "integer" }, "profilename": { "type": "string" }, "clientmac": { "type": "string" }, "clientip": { "type": "string", "format": "ipv4" }, "clientname": { "type": "string" }, "ap_mac": { "type": "string" }, "ap_ip": { "type": "string" }, "apname": { "type": "string" }, "channel": { "type": "integer" }, "rssi": { "type": "integer" }, "bitrate": { "type": "integer" }, "txpower": { "type": "integer" }, "dtim": { "type": "integer" }, "beaconperiod": { "type": "integer" }, "ssid": { "type": "string" }, "currentradiomode": { "type": "integer" } }, "required": [ "cardstate", "profilename", "clientmac", "clientip", "clientname", "ap_mac", "ap_ip", "apname", "channel", "rssi", "bitrate", "txpower", "dtim", "beaconperiod", "ssid", "currentradiomode" ] } }, "required": [ "result" ] }; @injectable() export class wlanservice { private getstatusurl = 'app/getwlanstatus'; // url web api constructor(private http: http, private validator: jsonvalidatorservice) { } scan(): void { console.log("scanning aps...") } getstatus(): observable<wlanstatus> { return this.issuerequest(this.getstatusurl, wlanstatusschema); } private issuerequest(requesturl: string, schema: any): observable<object> { return this.http.get(requesturl) .map((res: response) => { let body = res.json(); let valid = this.validator.validate(schema, body.data); if (!valid) { throw (new typeerror("not valid response: " + json.stringify(this.validator.geterrors()))); } return body.data.result; }) .catch(this.handleerror); } private handleerror(error: response | any) { // in real world app, might use remote logging infrastructure let errmsg: string; if (error instanceof response) { const body = error.json() || ''; const err = body.error || json.stringify(body); errmsg = `${error.status} - ${error.statustext || ''} ${err}`; } else { errmsg = error.message ? error.message : error.tostring(); } console.error(errmsg); return observable.throw(errmsg); } }
shouldn't setting class files use validate schema? visual studio show such errors create the.
something export class hero { id: number; name: string; }
Comments
Post a Comment