diff --git a/packages/nmea-parser/package.json b/packages/nmea-parser/package.json index 2431add..c2eee98 100644 --- a/packages/nmea-parser/package.json +++ b/packages/nmea-parser/package.json @@ -1,6 +1,6 @@ { "name": "@coremarine/nmea-parser", - "version": "1.6.15", + "version": "1.7.0", "description": "Library to parse NMEA 0183 sentences", "author": "CoreMarine", "license": "MIT", @@ -50,12 +50,13 @@ "test:coverage": "vitest run --coverage" }, "dependencies": { - "@gcornut/valibot-json-schema": "^0.0.28", - "@schemasjs/valibot-numbers": "^1.0.7", + "@gcornut/valibot-json-schema": "^0.31.0", + "@schemasjs/valibot-numbers": "^1.0.13", + "@schemasjs/validator": "^1.0.1", "js-yaml": "4.1.0" }, "peerDependencies": { - "valibot": ">=0.30.0" + "valibot": ">=0.31.0" }, "devDependencies": { "@types/js-yaml": "4.0.9" diff --git a/packages/nmea-parser/src/nmea.ts b/packages/nmea-parser/src/nmea.ts index f8c058f..e8fb9f2 100644 --- a/packages/nmea-parser/src/nmea.ts +++ b/packages/nmea-parser/src/nmea.ts @@ -1,165 +1,165 @@ export const PROTOCOLS = { - protocols: [ + "protocols": [ { - protocol: 'NMEA', - version: '3.1', - standard: true, - sentences: [ + "protocol": "NMEA", + "version": "3.1", + "standard": true, + "sentences": [ { - sentence: 'AAM', - description: 'Waypoint Arrival Alarm', - fields: [ + "sentence": "AAM", + "description": "Waypoint Arrival Alarm", + "fields": [ { - name: 'status', - type: 'string', - note: 'BOOLEAN\n\nA = arrival circle entered\n\nV = arrival circle not passed' + "name": "status", + "type": "string", + "note": "BOOLEAN\n\nA = arrival circle entered\n\nV = arrival circle not passed" }, { - name: 'status', - type: 'string', - note: 'BOOLEAN\n\nA = perpendicular passed at waypoint\n\nV = perpendicular not passed' + "name": "status", + "type": "string", + "note": "BOOLEAN\n\nA = perpendicular passed at waypoint\n\nV = perpendicular not passed" }, { - name: 'arrival_circle_radius', - type: 'float32' + "name": "arrival_circle_radius", + "type": "float32" }, { - name: 'radius_units', - type: 'string', - units: 'nautic miles' + "name": "radius_units", + "type": "string", + "units": "nautic miles" }, { - name: 'waypoint_id', - type: 'string' + "name": "waypoint_id", + "type": "string" } ] }, { - sentence: 'GGA', - description: 'Global Positioning System Fix Data', - fields: [ + "sentence": "GGA", + "description": "Global Positioning System Fix Data", + "fields": [ { - name: 'utc_position', - type: 'string', - units: 'ms' + "name": "utc_position", + "type": "string", + "units": "ms" }, { - name: 'latitude', - type: 'string', - units: 'deg' + "name": "latitude", + "type": "string", + "units": "deg" }, { - name: 'latitude_direction', - type: 'string', - note: 'N: North\n S: South' + "name": "latitude_direction", + "type": "string", + "note": "N: North\n S: South" }, { - name: 'longitude', - type: 'string', - units: 'deg' + "name": "longitude", + "type": "string", + "units": "deg" }, { - name: 'longitude_direction', - type: 'string', - note: 'E - East\n W - West' + "name": "longitude_direction", + "type": "string", + "note": "E - East\n W - West" }, { - name: 'gps_quality', - type: 'int8', - note: '0: Fix not valid\n 1: GPS fix\n 2: Differential GPS fix (DGNSS), SBAS, OmniSTAR VBS, Beacon, RTX in GVBS mode\n 3: Not applicable\n 4: RTK Fixed, xFill\n 5: RTK Float, OmniSTAR XP/HP, Location RTK, RTX\n 6: INS Dead reckoning\n 7: Manual Input Mode\n 8: Simulator Mode' + "name": "gps_quality", + "type": "int8", + "note": "0: Fix not valid\n 1: GPS fix\n 2: Differential GPS fix (DGNSS), SBAS, OmniSTAR VBS, Beacon, RTX in GVBS mode\n 3: Not applicable\n 4: RTK Fixed, xFill\n 5: RTK Float, OmniSTAR XP/HP, Location RTK, RTX\n 6: INS Dead reckoning\n 7: Manual Input Mode\n 8: Simulator Mode" }, { - name: 'satellites', - type: 'uint8' + "name": "satellites", + "type": "uint8" }, { - name: 'hdop', - type: 'float64' + "name": "hdop", + "type": "float64" }, { - name: 'altitude', - type: 'float64', - units: 'm', - note: 'Orthometric height Mean-Sea-Level (MSL reference)' + "name": "altitude", + "type": "float64", + "units": "m", + "note": "Orthometric height Mean-Sea-Level (MSL reference)" }, { - name: 'altitude_units', - type: 'string', - units: 'm' + "name": "altitude_units", + "type": "string", + "units": "m" }, { - name: 'geoid_separation', - type: 'float64', - units: 'm', - note: 'Geoidal Separation: the difference between the WGS-84 earth ellipsoid surface and mean-sea-level (geoid) surface, "-" = mean-sea-level surface below WGS-84 ellipsoid surface.' + "name": "geoid_separation", + "type": "float64", + "units": "m", + "note": "Geoidal Separation: the difference between the WGS-84 earth ellipsoid surface and mean-sea-level (geoid) surface, \"-\" = mean-sea-level surface below WGS-84 ellipsoid surface." }, { - name: 'geoid_separation_units', - type: 'string', - units: 'm' + "name": "geoid_separation_units", + "type": "string", + "units": "m" }, { - name: 'age_of_differential_gps_data', - type: 'uint32', - units: 'sec', - note: 'Time in seconds since last SC104 Type 1 or 9 update, null field when DGPS is not used300' + "name": "age_of_differential_gps_data", + "type": "uint32", + "units": "sec", + "note": "Time in seconds since last SC104 Type 1 or 9 update, null field when DGPS is not used300" }, { - name: 'reference_station_id', - type: 'uint16', - note: 'Reference station ID, range 0000 to 4095. A null field when any reference station ID is selected and no corrections are received. See table below for a description of the field values.\n\n0002 CenterPoint or ViewPoint RTX\n\n0005 RangePoint RTX\n\n0006 FieldPoint RTX\n\n0100 VBS\n\n1000 HP\n\n1001 HP/XP (Orbits)\n\n1002 HP/G2 (Orbits)\n\n1008 XP (GPS)\n\n1012 G2 (GPS)\n\n1013 G2 (GPS/GLONASS)\n\n1014 G2 (GLONASS)\n\n1016 HP/XP (GPS)\n\n1020 HP/G2 (GPS)\n\n1021 HP/G2 (GPS/GLONASS)' + "name": "reference_station_id", + "type": "uint16", + "note": "Reference station ID, range 0000 to 4095. A null field when any reference station ID is selected and no corrections are received. See table below for a description of the field values.\n\n0002 CenterPoint or ViewPoint RTX\n\n0005 RangePoint RTX\n\n0006 FieldPoint RTX\n\n0100 VBS\n\n1000 HP\n\n1001 HP/XP (Orbits)\n\n1002 HP/G2 (Orbits)\n\n1008 XP (GPS)\n\n1012 G2 (GPS)\n\n1013 G2 (GPS/GLONASS)\n\n1014 G2 (GLONASS)\n\n1016 HP/XP (GPS)\n\n1020 HP/G2 (GPS)\n\n1021 HP/G2 (GPS/GLONASS)" } ] }, { - sentence: 'HDT', - description: 'Heading - True', - fields: [ + "sentence": "HDT", + "description": "Heading - True", + "fields": [ { - name: 'heading', - type: 'float32', - note: 'Heading, degrees True' + "name": "heading", + "type": "float32", + "note": "Heading, degrees True" }, { - name: 'true', - type: 'string', - note: 'T = True' + "name": "true", + "type": "string", + "note": "T = True" } ] }, { - sentence: 'ZDA', - description: 'Time & Date - UTC, day, month, year and local time zone', - fields: [ + "sentence": "ZDA", + "description": "Time & Date - UTC, day, month, year and local time zone", + "fields": [ { - name: 'utc_time', - type: 'string', - note: 'UTC time (hours, minutes, seconds, may have fractional subseconds)' + "name": "utc_time", + "type": "string", + "note": "UTC time (hours, minutes, seconds, may have fractional subseconds)" }, { - name: 'day', - type: 'int8', - note: 'Day, 01 to 31' + "name": "day", + "type": "int8", + "note": "Day, 01 to 31" }, { - name: 'month', - type: 'int8', - note: 'Month, 01 to 12' + "name": "month", + "type": "int8", + "note": "Month, 01 to 12" }, { - name: 'year', - type: 'int16', - note: 'Year (4 digits)' + "name": "year", + "type": "int16", + "note": "Year (4 digits)" }, { - name: 'local_zone_hours', - type: 'int8', - note: 'Local zone description, 00 to +- 13 hours' + "name": "local_zone_hours", + "type": "int8", + "note": "Local zone description, 00 to +- 13 hours" }, { - name: 'local_zone_minutes', - type: 'int8', - note: 'Local zone minutes description, 00 to 59, apply same sign as local hours' + "name": "local_zone_minutes", + "type": "int8", + "note": "Local zone minutes description, 00 to 59, apply same sign as local hours" } ] } diff --git a/packages/nmea-parser/src/parser.ts b/packages/nmea-parser/src/parser.ts index ee2493b..0359e6c 100644 --- a/packages/nmea-parser/src/parser.ts +++ b/packages/nmea-parser/src/parser.ts @@ -1,21 +1,20 @@ -import * as v from 'valibot' import { END_FLAG, END_FLAG_LENGTH, MAX_CHARACTERS, NMEA_ID_LENGTH, START_FLAG, START_FLAG_LENGTH } from './constants' -import { BooleanSchema, NMEALikeSchema, ProtocolsInputSchema, StringSchema, UnsignedIntegerSchema } from './schemas' -import type { Data, FieldType, FieldUnknown, NMEAKnownSentence, NMEALike, NMEAParser, NMEAPreParsed, NMEASentence, NMEAUknownSentence, ParserSentences, ProtocolOutput, ProtocolsFile, ProtocolsInput, Sentence, StoredSentences } from './types' +import { PROTOCOLS } from './nmea' import { getSentencesByProtocol, getStoreSentences, readProtocolsFile, readProtocolsString } from './protocols' +import { BooleanSchema, NMEALikeSchema, ProtocolsInputSchema, StringSchema, UnsignedIntegerSchema } from './schemas' import { generateSentenceFromModel, getFakeSentence, getNMEAUnparsedSentence } from './sentences' +import type { Data, FieldType, FieldUnknown, NMEAKnownSentence, NMEALike, NMEAParser, NMEAPreParsed, NMEASentence, NMEAUknownSentence, ParserSentences, ProtocolOutput, ProtocolsFile, ProtocolsInput, Sentence, StoredSentences } from './types' import { getTalker } from './utils' -import { PROTOCOLS } from './nmea' export class Parser implements NMEAParser { // Memory - Buffer protected _memory: boolean = true get memory (): typeof this._memory { return this._memory } - set memory (mem: boolean) { this._memory = v.parse(BooleanSchema, mem) } + set memory (mem: boolean) { this._memory = BooleanSchema.parse(mem) } protected _buffer: string = '' protected _bufferLength: number = MAX_CHARACTERS get bufferLimit (): typeof this._bufferLength { return this._bufferLength } - set bufferLimit (limit: number) { this._bufferLength = v.parse(UnsignedIntegerSchema, limit) } + set bufferLimit (limit: number) { this._bufferLength = UnsignedIntegerSchema.parse(limit) } // Sentences protected _sentences: StoredSentences = new Map() // get sentences() { return this._sentences } @@ -28,7 +27,7 @@ export class Parser implements NMEAParser { } private readInternalProtocols (): void { - const parsed = v.parse(ProtocolsInputSchema, PROTOCOLS) + const parsed = ProtocolsInputSchema.parse(PROTOCOLS) this.addProtocols(parsed) } @@ -44,7 +43,7 @@ export class Parser implements NMEAParser { } getSentence (id: string): Sentence { - if (!v.is(StringSchema, id) || id.length < NMEA_ID_LENGTH) { return null } + if (!StringSchema.is(id) || id.length < NMEA_ID_LENGTH) { return null } const aux = this._sentences.get(id) ?? null if (aux !== null) { return aux } const [talk, sent] = [id.slice(0, id.length - NMEA_ID_LENGTH), id.slice(-NMEA_ID_LENGTH)] @@ -55,7 +54,7 @@ export class Parser implements NMEAParser { } addProtocols (input: ProtocolsInput): void { - if (!v.is(ProtocolsInputSchema, input)) { + if (!ProtocolsInputSchema.is(input)) { const error = 'Parser: invalid protocols to parse' console.error(error) console.error(input) @@ -73,7 +72,7 @@ export class Parser implements NMEAParser { } getFakeSentenceByID (id: string): NMEALike | null { - if (!v.is(StringSchema, id) || id.length < NMEA_ID_LENGTH) { return null } + if (!StringSchema.is(id) || id.length < NMEA_ID_LENGTH) { return null } const aux = this._sentences.get(id) ?? null if (aux !== null) { return generateSentenceFromModel(aux) } // const [_, sent] = [id.slice(0, id.length - NMEA_ID_LENGTH), id.slice(-NMEA_ID_LENGTH)] @@ -85,7 +84,7 @@ export class Parser implements NMEAParser { } parseData (text: string): NMEASentence[] { - if (!v.is(StringSchema, text)) return [] + if (!StringSchema.is(text)) return [] const data = (this.memory) ? this._buffer + text : text return this.getFrames(data) } @@ -177,7 +176,7 @@ export class Parser implements NMEAParser { if (this._sentences.has(preparsedSentence.sentence)) { const sentence = this.getKnownFrame(preparsedSentence) if (sentence !== null) return sentence - // Probably known sentence by talker ID + // Probably known sentence by talker ID } else { const sentence = this.getKnownTalkerFrame(preparsedSentence) if (sentence !== null) return sentence @@ -207,7 +206,7 @@ export class Parser implements NMEAParser { const possibleFrame = text.slice(start, end + END_FLAG_LENGTH) - if (!v.is(NMEALikeSchema, possibleFrame)) { + if (!NMEALikeSchema.is(possibleFrame)) { pivot = start + START_FLAG_LENGTH continue } diff --git a/packages/nmea-parser/src/protocols.ts b/packages/nmea-parser/src/protocols.ts index f6e5b6b..e94cce3 100644 --- a/packages/nmea-parser/src/protocols.ts +++ b/packages/nmea-parser/src/protocols.ts @@ -1,29 +1,16 @@ import fs from 'node:fs' -import * as v from 'valibot' -// import Path from 'node:path' -// import zodToJsonSchema from 'zod-to-json-schema' import yaml from 'js-yaml' import { ProtocolsFileSchema, StringSchema } from './schemas' import type { Protocol, ProtocolOutput, ProtocolsFile, StoredSentence, StoredSentences } from './types' -// export const jsonSchema = zodToJsonSchema(ProtocolsFileSchema, 'NMEAProtocolsSchema') -// export const jsonSchema = zodToJsonSchema(ProtocolsFileSchema, 'NMEAProtocolsSchema') - -// export const createJSONSchema = (input: JSONSchemaInput): void => { -// const { path, filename } = JSONSchemaInputSchema.parse(input) -// const FILE = Path.join(path, filename) -// const CONTENT = JSON.stringify(jsonSchema, null, 2) -// fs.writeFileSync(FILE, CONTENT) -// } - export const readProtocolsString = (content: string): ProtocolsFile => { const fileData = yaml.load(content) - return v.parse(ProtocolsFileSchema, fileData) + return ProtocolsFileSchema.parse(fileData) } export const readProtocolsFile = (file: string): ProtocolsFile => { - const filename = v.parse(StringSchema, file) + const filename = StringSchema.parse(file) const content = fs.readFileSync(filename, 'utf-8') return readProtocolsString(content) } diff --git a/packages/nmea-parser/src/schemas.ts b/packages/nmea-parser/src/schemas.ts index a8c1280..e1c7228 100644 --- a/packages/nmea-parser/src/schemas.ts +++ b/packages/nmea-parser/src/schemas.ts @@ -1,15 +1,42 @@ import * as v from 'valibot' +import { ValibotValidator } from '@schemasjs/validator' +import { + IntegerSchema as ValibotIntegerSchema, + Int8Schema as ValibotInt8Schema, + Int16Schema as ValibotInt16Schema, + Int32Schema as ValibotInt32Schema, + UnsignedIntegerSchema as ValibotUnsignedIntegerSchema, + Uint8Schema as ValibotUint8Schema, + Uint16Schema as ValibotUint16Schema, + Uint32Schema as ValibotUint32Schema +} from '@schemasjs/valibot-numbers' import { DELIMITER, END_FLAG, SEPARATOR, START_FLAG } from './constants' // COMMONS -export const StringSchema = v.string() -export const StringArraySchema = v.array(StringSchema) -export const BooleanSchema = v.boolean() -export const NumberSchema = v.number() -export const UnsignedIntegerSchema = v.number([v.integer(), v.minValue(0)]) +const ValibotStringSchema = v.string() +export const StringSchema = ValibotValidator>(ValibotStringSchema) + +const ValibotStringArraySchema = v.array(ValibotStringSchema) +export const StringArraySchema = ValibotValidator>(ValibotStringArraySchema) + +const ValibotBooleanSchema = v.boolean() +export const BooleanSchema = ValibotValidator>(ValibotBooleanSchema) + +const ValibotNumberSchema = v.number() +export const NumberSchema = ValibotValidator>(ValibotNumberSchema) + +export const IntegerSchema = ValibotValidator>(ValibotIntegerSchema) +export const Int8Schema = ValibotValidator>(ValibotInt8Schema) +export const Int16Schema = ValibotValidator>(ValibotInt16Schema) +export const Int32Schema = ValibotValidator>(ValibotInt32Schema) + +export const UnsignedIntegerSchema = ValibotValidator>(ValibotUnsignedIntegerSchema) +export const Uint8Schema = ValibotValidator>(ValibotUint8Schema) +export const Uint16Schema = ValibotValidator>(ValibotUint16Schema) +export const Uint32Schema = ValibotValidator>(ValibotUint32Schema) // PROTOCOLS -export const FieldTypeSchema = v.union([ +const ValibotFieldTypeSchema = v.union([ // Numbers v.literal('char'), v.literal('uint8'), v.literal('signed char'), v.literal('int8'), @@ -32,150 +59,177 @@ export const FieldTypeSchema = v.union([ v.literal('bool'), v.literal('boolean') ]) +export const FieldTypeSchema = ValibotValidator>(ValibotFieldTypeSchema) -export const FieldSchema = v.object({ - name: StringSchema, - type: FieldTypeSchema, - units: v.optional(StringSchema), - note: v.optional(StringSchema) +const ValibotFieldSchema = v.object({ + name: ValibotStringSchema, + type: ValibotFieldTypeSchema, + units: v.optional(ValibotStringSchema), + note: v.optional(ValibotStringSchema) }) +export const FieldSchema = ValibotValidator>(ValibotFieldSchema) -export const FieldUnknownSchema = v.object({ +const ValibotFieldUnknownSchema = v.object({ name: v.literal('unknown'), type: v.literal('string'), - data: StringSchema + data: ValibotStringSchema }) +export const FieldUnknownSchema = ValibotValidator>(ValibotFieldUnknownSchema) -export const ProtocolSentenceSchema = v.object({ - sentence: StringSchema, - fields: v.array(FieldSchema), - description: v.optional(StringSchema) +const ValibotProtocolSentenceSchema = v.object({ + sentence: ValibotStringSchema, + fields: v.array(ValibotFieldSchema), + description: v.optional(ValibotStringSchema) }) +export const ProtocolSentenceSchema = ValibotValidator>(ValibotProtocolSentenceSchema) -const maxThreeFields = v.custom((val: string) => val.split('.').length < 4, 'VersionSchema: more than 3 fields') -const validMajor = v.custom((val: string) => { +const ValibotMaxThreeFields = v.check<`${number}.${number}.${number}`, 'VersionSchema: more than 3 fields'>((input: string) => ( + input.split('.').length < 4 +), 'VersionSchema: more than 3 fields') +const ValibotValidMajor = v.check<`${number}.${number}.${number}`, 'VersionSchema: Invalid major'>((val: string) => { const major = Number(val.split('.')[0]) return !Number.isNaN(major) && major > 0 }, 'VersionSchema: Invalid major') -const validMinor = v.custom((val: string) => { +const ValibotValidMinor = v.check<`${number}.${number}.${number}`, 'VersionSchema: Invalid major'>((val: string) => { const fields = val.split('.') if (fields.length < 2) return true const minor = Number(fields[1]) return !Number.isNaN(minor) && minor > 0 }, 'VersionSchema: Invalid major') -const validPatch = v.custom((val: string) => { +const ValibotValidPatch = v.check<`${number}.${number}.${number}`, 'VersionSchema: Invalid patch'>((val: string) => { const fields = val.split('.') if (fields.length < 3) return true const patch = Number.parseInt(fields[2]) return !Number.isNaN(patch) && patch > 0 }, 'VersionSchema: Invalid patch') - -export const VersionSchema = v.special<`${number}.${number}.${number}`>( - val => v.safeParse(StringSchema, val).success, - [maxThreeFields, validMajor, validMinor, validPatch] as v.Pipe<`${number}.${number}.${number}`> +const ValibotVersionSchema = v.pipe( + v.custom<`${number}.${number}.${number}`>(val => v.is(ValibotStringSchema, val)), + ValibotMaxThreeFields, + ValibotValidMajor, + ValibotValidMinor, + ValibotValidPatch ) +export const VersionSchema = ValibotValidator>(ValibotVersionSchema) -export const ProtocolSchema = v.object({ - protocol: StringSchema, - version: v.optional(VersionSchema), - standard: v.optional(BooleanSchema, false), - sentences: v.array(ProtocolSentenceSchema) +const ValibotProtocolSchema = v.object({ + protocol: ValibotStringSchema, + version: v.optional(ValibotVersionSchema), + standard: v.optional(ValibotBooleanSchema, false), + sentences: v.array(ValibotProtocolSentenceSchema) }) +export const ProtocolSchema = ValibotValidator>(ValibotProtocolSchema) -export const ProtocolsFileSchema = v.object({ protocols: v.array(ProtocolSchema) }) +const ValibotProtocolsFileSchema = v.object({ protocols: v.array(ValibotProtocolSchema) }) +export const ProtocolsFileSchema = ValibotValidator>(ValibotProtocolsFileSchema) -export const ProtocolsInputSchema = v.object({ - file: v.optional(StringSchema), - content: v.optional(StringSchema), - protocols: v.optional(v.array(ProtocolSchema)) +const ValibotProtocolsInputSchema = v.object({ + file: v.optional(ValibotStringSchema), + content: v.optional(ValibotStringSchema), + protocols: v.optional(v.array(ValibotProtocolSchema)) }) +export const ProtocolsInputSchema = ValibotValidator>(ValibotProtocolsInputSchema) -export const StoredSentenceSchema = v.object({ - sentence: StringSchema, +const ValibotStoredSentenceSchema = v.object({ + sentence: ValibotStringSchema, protocol: v.object({ - name: StringSchema, - standard: v.optional(BooleanSchema, false), - version: v.optional(VersionSchema) + name: ValibotStringSchema, + standard: v.optional(ValibotBooleanSchema, false), + version: v.optional(ValibotVersionSchema) }), - fields: v.array(FieldSchema), - description: v.optional(StringSchema) + fields: v.array(ValibotFieldSchema), + description: v.optional(ValibotStringSchema) }) +export const StoredSentenceSchema = ValibotValidator>(ValibotStoredSentenceSchema) -export const StoredSentencesSchema = v.map(StringSchema, StoredSentenceSchema) +const ValibotStoredSentencesSchema = v.map(ValibotStringSchema, ValibotStoredSentenceSchema) +export const StoredSentencesSchema = ValibotValidator>(ValibotStoredSentencesSchema) -export const JSONSchemaInputSchema = v.object({ - path: v.optional(StringSchema), - filename: v.optional(StringSchema, 'nmea_protocols_schema.json') +const ValibotJSONSchemaInputSchema = v.object({ + path: v.optional(ValibotStringSchema), + filename: v.optional(ValibotStringSchema, 'nmea_protocols_schema.json') }) +export const JSONSchemaInputSchema = ValibotValidator>(ValibotJSONSchemaInputSchema) // SENTENCES -export const NMEALikeSchema = v.string([ +const ValibotNMEALikeSchema = v.pipe( + v.string(), v.startsWith(START_FLAG), v.includes(SEPARATOR), v.includes(DELIMITER), v.endsWith(END_FLAG) -]) +) +export const NMEALikeSchema = ValibotValidator>(ValibotNMEALikeSchema) -export const TalkerSchema = v.object({ - id: StringSchema, - description: StringSchema +const ValibotTalkerSchema = v.object({ + id: ValibotStringSchema, + description: ValibotStringSchema }) +export const TalkerSchema = ValibotValidator>(ValibotTalkerSchema) -export const NMEAUnparsedSentenceSchema = v.object({ - raw: StringSchema, - sentence: StringSchema, - checksum: UnsignedIntegerSchema, - data: StringArraySchema +const ValibotNMEAUnparsedSentenceSchema = v.object({ + raw: ValibotStringSchema, + sentence: ValibotStringSchema, + checksum: ValibotUnsignedIntegerSchema, + data: ValibotStringArraySchema }) +export const NMEAUnparsedSentenceSchema = ValibotValidator>(ValibotNMEAUnparsedSentenceSchema) -export const NMEAPreParsedSentenceSchema = v.merge([ - NMEAUnparsedSentenceSchema, +const ValibotNMEAPreParsedSentenceSchema = v.intersect([ + ValibotNMEAUnparsedSentenceSchema, v.object({ - timestamp: UnsignedIntegerSchema, - talker: v.optional(v.nullable(TalkerSchema), null) + timestamp: ValibotUnsignedIntegerSchema, + talker: v.optional(v.nullable(ValibotTalkerSchema), null) }) ]) +export const NMEAPreParsedSentenceSchema = ValibotValidator>(ValibotNMEAPreParsedSentenceSchema) -export const DataSchema = v.nullable(v.union([StringSchema, NumberSchema, BooleanSchema])) +const ValibotDataSchema = v.nullable(v.union([ValibotStringSchema, ValibotNumberSchema, ValibotBooleanSchema])) +export const DataSchema = ValibotValidator>(ValibotDataSchema) -export const FieldParsedSchema = v.merge([ - FieldSchema, +const ValibotFieldParsedSchema = v.intersect([ + ValibotFieldSchema, v.object({ - data: DataSchema + data: ValibotDataSchema }) ]) +export const FieldParsedSchema = ValibotValidator>(ValibotFieldParsedSchema) -export const StoredSentenceDataSchema = v.merge([ - StoredSentenceSchema, +const ValibotStoredSentenceDataSchema = v.intersect([ + ValibotStoredSentenceSchema, v.object({ - fields: v.array(FieldParsedSchema), - data: v.array(DataSchema) + fields: v.array(ValibotFieldParsedSchema), + data: v.array(ValibotDataSchema) }) ]) +export const StoredSentenceDataSchema = ValibotValidator>(ValibotStoredSentenceDataSchema) -export const NMEAUknownSentenceSchema = v.merge([ - NMEAPreParsedSentenceSchema, +const ValibotNMEAUknownSentenceSchema = v.intersect([ + ValibotNMEAPreParsedSentenceSchema, v.object({ protocol: v.object({ name: v.literal('UNKNOWN') }), - fields: v.array(FieldUnknownSchema) + fields: v.array(ValibotFieldUnknownSchema) }) ]) +export const NMEAUknownSentenceSchema = ValibotValidator>(ValibotNMEAUknownSentenceSchema) -export const NMEAKnownSentenceSchema = v.merge([ - StoredSentenceDataSchema, +const ValibotNMEAKnownSentenceSchema = v.intersect([ + ValibotStoredSentenceDataSchema, v.object({ - timestamp: UnsignedIntegerSchema, - talker: v.optional(v.nullable(TalkerSchema), null), - checksum: UnsignedIntegerSchema, - fields: v.array(FieldParsedSchema), - data: v.array(DataSchema) + timestamp: ValibotUnsignedIntegerSchema, + talker: v.optional(v.nullable(ValibotTalkerSchema), null), + checksum: ValibotUnsignedIntegerSchema, + fields: v.array(ValibotFieldParsedSchema), + data: v.array(ValibotDataSchema) }) ]) +export const NMEAKnownSentenceSchema = ValibotValidator>(ValibotNMEAKnownSentenceSchema) -export const NMEASentenceSchema = v.union([NMEAKnownSentenceSchema, NMEAUknownSentenceSchema]) +const ValibotNMEASentenceSchema = v.union([ValibotNMEAKnownSentenceSchema, ValibotNMEAUknownSentenceSchema]) +export const NMEASentenceSchema = ValibotValidator>(ValibotNMEASentenceSchema) -export const OutputSentenceSchema = v.merge([ - StoredSentenceSchema, +const ValibotOutputSentenceSchema = v.intersect([ + ValibotStoredSentenceSchema, v.object({ - talker: v.optional(TalkerSchema) + talker: v.optional(ValibotTalkerSchema) }) ]) +export const OutputSentenceSchema = ValibotValidator>(ValibotOutputSentenceSchema) diff --git a/packages/nmea-parser/src/sentences.ts b/packages/nmea-parser/src/sentences.ts index 3217e0b..14a48fa 100644 --- a/packages/nmea-parser/src/sentences.ts +++ b/packages/nmea-parser/src/sentences.ts @@ -1,4 +1,3 @@ -import * as v from 'valibot' import { getChecksum, numberChecksumToString, stringChecksumToNumber } from './checksum' import { CHECKSUM_LENGTH, DELIMITER, END_FLAG, END_FLAG_LENGTH, MINIMAL_LENGTH, SEPARATOR, START_FLAG, START_FLAG_LENGTH } from './constants' import { NMEALikeSchema, NMEAUknownSentenceSchema, NMEAUnparsedSentenceSchema } from './schemas' @@ -7,9 +6,9 @@ import { isLowerCharASCII, isNumberCharASCII, isUpperCharASCII } from './utils' // GET NMEA SENTENCE export const isNMEAFrame = (text: string): boolean => { // Not valid NMEA like - const parsed = v.safeParse(NMEALikeSchema, text) + const parsed = NMEALikeSchema.safeParse(text) if (!parsed.success) { - console.debug(`Error parsing frame -> ${parsed.issues[0].message}`) + console.debug(`Error parsing frame -> ${(parsed.errors as string[])[0]}`) return false } // More than one START Flag @@ -18,7 +17,7 @@ export const isNMEAFrame = (text: string): boolean => { return false } // Not enough characters - const { output: data } = parsed + const data = parsed.data as string if (data.length < MINIMAL_LENGTH) { console.debug('Invalid NMEA line -> it doesn\'t contain any data') return false @@ -50,19 +49,19 @@ export const getNMEAUnparsedSentence = (text: string): NMEAUnparsedSentence | nu const [info, cs] = raw.slice(1, -END_FLAG_LENGTH).split(DELIMITER) const checksum = stringChecksumToNumber(cs) const [sentence, ...data] = info.split(SEPARATOR) - const parsed = v.safeParse(NMEAUnparsedSentenceSchema, { raw, sentence, checksum, data }) - if (parsed.success) { return parsed.output } + const parsed = NMEAUnparsedSentenceSchema.safeParse({ raw, sentence, checksum, data }) + if (parsed.success) { return (parsed.data as NMEAUnparsedSentence) } console.debug(`Error parsing sentence -> ${raw}`) - console.debug(parsed.issues[0].message) + console.debug((parsed.errors as string[])[0]) return null } export const getUnknownSentence = (sentence: NMEAPreParsed): NMEAUknownSentence => { const fields: FieldUnknown[] = sentence.data.map(value => ({ name: 'unknown', type: 'string', data: value })) const unknowFrame = { ...sentence, protocol: { name: 'UNKNOWN' }, fields } - const parsed = v.safeParse(NMEAUknownSentenceSchema, unknowFrame) - if (parsed.success) return parsed.output - throw new Error(parsed.issues[0].message) + const parsed = NMEAUknownSentenceSchema.safeParse(unknowFrame) + if (parsed.success) return parsed.data as NMEAUknownSentence + throw new Error((parsed.errors as string[])[0]) } // TESTING - GENERATE @@ -84,10 +83,6 @@ export const getNumberValue = (type: FieldType): number => { case 'unsigned int': return (new Uint32Array([useed]))[0] - // case 'uint64': - // case 'unsigned long': - // return Number((new BigUint64Array([BigInt(Math.floor(seed))]))[0]) - case 'int8': case 'signed char': return (new Int8Array([seed]))[0] @@ -100,10 +95,6 @@ export const getNumberValue = (type: FieldType): number => { case 'int': return (new Int32Array([seed]))[0] - // case 'int64': - // case 'long': - // return Number((new BigInt64Array([BigInt(Math.floor(seed))]))[0]) - case 'float32': case 'float': return (new Float32Array([fseed]))[0] diff --git a/packages/nmea-parser/src/types.ts b/packages/nmea-parser/src/types.ts index 6927ddf..4c4d332 100644 --- a/packages/nmea-parser/src/types.ts +++ b/packages/nmea-parser/src/types.ts @@ -1,39 +1,54 @@ -import { Input, Output } from 'valibot' import type { - FieldTypeSchema, FieldSchema, - ProtocolSchema, ProtocolsFileSchema, ProtocolSentenceSchema, - VersionSchema, JSONSchemaInputSchema, - StoredSentenceSchema, StoredSentencesSchema, - NMEALikeSchema, NMEAUnparsedSentenceSchema, NMEAPreParsedSentenceSchema, - DataSchema, FieldParsedSchema, NMEASentenceSchema, NMEAUknownSentenceSchema, NMEAKnownSentenceSchema, ProtocolsInputSchema, FieldUnknownSchema, OutputSentenceSchema, TalkerSchema, StoredSentenceDataSchema + DataSchema, + FieldParsedSchema, + FieldSchema, + FieldTypeSchema, + FieldUnknownSchema, + JSONSchemaInputSchema, + NMEAKnownSentenceSchema, + NMEALikeSchema, + NMEAPreParsedSentenceSchema, + NMEASentenceSchema, + NMEAUknownSentenceSchema, + NMEAUnparsedSentenceSchema, + OutputSentenceSchema, + ProtocolSchema, + ProtocolSentenceSchema, + ProtocolsFileSchema, + ProtocolsInputSchema, + StoredSentenceDataSchema, + StoredSentenceSchema, + StoredSentencesSchema, + TalkerSchema, + VersionSchema } from './schemas' // COMMONS // PROTOCOLS -export type FieldType = Input -export type Field = Input -export type FieldUnknown = Input -export type ProtocolSentence = Input -export type Version = Input -export type Protocol = Output -export type ProtocolsFile = Output -export type ProtocolsInput = Output -export type StoredSentence = Input -export type StoredSentences = Input +export type FieldType = ReturnType +export type Field = ReturnType +export type FieldUnknown = ReturnType +export type ProtocolSentence = ReturnType +export type Version = ReturnType +export type Protocol = ReturnType +export type ProtocolsFile = ReturnType +export type ProtocolsInput = ReturnType +export type StoredSentence = ReturnType +export type StoredSentences = ReturnType export type ParserSentences = Record // JSON Schema -export type JSONSchemaInput = Input +export type JSONSchemaInput = ReturnType // SENTENCES -export type NMEALike = Input -export type Talker = Input -export type NMEAUnparsedSentence = Input -export type NMEAPreParsed = Input -export type Data = Input -export type FieldParsed = Input -export type StoredSentenceData = Input -export type NMEAUknownSentence = Input -export type NMEAKnownSentence = Input -export type NMEASentence = Input -export type OutputSentence = Input +export type NMEALike = ReturnType +export type Talker = ReturnType +export type NMEAUnparsedSentence = ReturnType +export type NMEAPreParsed = ReturnType +export type Data = ReturnType +export type FieldParsed = ReturnType +export type StoredSentenceData = ReturnType +export type NMEAUknownSentence = ReturnType +export type NMEAKnownSentence = ReturnType +export type NMEASentence = ReturnType +export type OutputSentence = ReturnType export type Sentence = null | OutputSentence // PARSER export interface ProtocolOutput { diff --git a/packages/nmea-parser/tests/index.test.ts b/packages/nmea-parser/tests/index.test.ts index 15ef496..a7dff3d 100644 --- a/packages/nmea-parser/tests/index.test.ts +++ b/packages/nmea-parser/tests/index.test.ts @@ -1,7 +1,6 @@ import fs from 'node:fs' import path from 'node:path' import { describe, test, expect } from 'vitest' -import * as v from 'valibot' import { NMEAParser as Parser } from '../src' import { generateSentenceFromModel, getFakeSentence } from '../src/sentences' import { NMEAKnownSentenceSchema, NMEALikeSchema, NMEAUknownSentenceSchema } from '../src/schemas' @@ -133,9 +132,9 @@ describe('Parser', () => { const input = generateSentenceFromModel(storedSentence) expect(input).toBeTypeOf('string') const output = parser.parseData(input)[0] - const parsed = v.safeParse(NMEAKnownSentenceSchema, output) + const parsed = NMEAKnownSentenceSchema.safeParse(output) if (!parsed.success) { - console.error(parsed.issues[0].message) + console.error((parsed.errors as string[])[0]) } expect(parsed.success).toBeTruthy() }) @@ -197,9 +196,9 @@ describe('Parser', () => { console.error(`Problem parsing frame -> ${input}`) expect(output).toHaveLength(1) } else { - const parsed = v.safeParse(NMEAUknownSentenceSchema, output[0]) + const parsed = NMEAUknownSentenceSchema.safeParse(output[0]) if (!parsed.success) { - console.error(parsed.issues[0].message) + console.error((parsed.errors as string[])[0]) } expect(parsed.success).toBeTruthy() } @@ -242,7 +241,7 @@ describe('Parser', () => { const id = sentences[key].sentence const fakeSentence = parser.getFakeSentenceByID(id) expect(fakeSentence).not.toBeNull() - expect(v.is(NMEALikeSchema, fakeSentence)).toBeTruthy() + expect(NMEALikeSchema.is(fakeSentence)).toBeTruthy() if (fakeSentence !== null) { const parsed = parser.parseData(fakeSentence) expect(parsed).toHaveLength(1) @@ -260,7 +259,7 @@ describe('Parser', () => { const id = talker + sentence const fakeSentence = parser.getFakeSentenceByID(id) expect(fakeSentence).not.toBeNull() - expect(v.is(NMEALikeSchema, fakeSentence)).toBeTruthy() + expect(NMEALikeSchema.is(fakeSentence)).toBeTruthy() if (fakeSentence !== null) { const starts = fakeSentence.startsWith(talker, 1) expect(starts).toBeTruthy() diff --git a/packages/nmea-parser/tests/parser.test.ts b/packages/nmea-parser/tests/parser.test.ts index fd83438..88bc4f9 100644 --- a/packages/nmea-parser/tests/parser.test.ts +++ b/packages/nmea-parser/tests/parser.test.ts @@ -1,7 +1,6 @@ import fs from 'node:fs' import path from 'node:path' import { describe, test, expect } from 'vitest' -import * as v from 'valibot' import { Parser } from '../src/parser' import { generateSentenceFromModel, getFakeSentence } from '../src/sentences' import { NMEAKnownSentenceSchema, NMEASentenceSchema, NMEAUknownSentenceSchema } from '../src/schemas' @@ -134,9 +133,9 @@ describe('Parser', () => { const input = generateSentenceFromModel(storedSentence) expect(input).toBeTypeOf('string') const output = parser.parseData(input)[0] - const parsed = v.safeParse(NMEAKnownSentenceSchema, output) + const parsed = NMEAKnownSentenceSchema.safeParse(output) if (!parsed.success) { - console.error(parsed.issues[0].message) + console.error((parsed.errors as string[])[0]) } expect(parsed.success).toBeTruthy() }) @@ -155,11 +154,11 @@ describe('Parser', () => { if (output.length === 2) { // Known talker ['GP', 'GA'].forEach((id, index) => { - const parsed = v.safeParse(NMEASentenceSchema, output[index]) - if (!parsed.success) { console.error(parsed.issues[0].message ) } + const parsed = NMEASentenceSchema.safeParse(output[index]) + if (!parsed.success) { console.error((parsed.errors as string[])[0] ) } expect(parsed.success).toBeTruthy() // @ts-ignore - const { output: data }: { output: NMEASentence} = parsed + const data = parsed.data as NMEASentence expect(data.talker).toEqual({ id, description: TALKERS.get(id)}) }) } @@ -183,8 +182,8 @@ describe('Parser', () => { expect(output).toHaveLength(2) } else { output.forEach(out => { - const parse = v.safeParse(NMEASentenceSchema, out) - if (!parse.success) { console.error(parse.issues[0].message ) } + const parse = NMEASentenceSchema.safeParse(out) + if (!parse.success) { console.error((parse.errors as string[])[0] ) } expect(parse.success).toBeTruthy() }) // Known special talker @@ -208,8 +207,8 @@ describe('Parser', () => { const output = parser.parseData(input); expect(output).toHaveLength(2) output.forEach(out => { - const parse = v.safeParse(NMEASentenceSchema, out) - if (!parse.success) { console.error(parse.issues[0].message ) } + const parse = NMEASentenceSchema.safeParse(out) + if (!parse.success) { console.error((parse.errors as string[])[0] ) } expect(parse.success).toBeTruthy() }) // Known special talker @@ -276,9 +275,9 @@ describe('Parser', () => { console.error(`Problem parsing frame -> ${input}`) expect(output).toHaveLength(1) } else { - const parsed = v.safeParse(NMEAUknownSentenceSchema, output[0]) + const parsed = NMEAUknownSentenceSchema.safeParse(output[0]) if (!parsed.success) { - console.error(parsed.issues[0].message) + console.error((parsed.errors as string[])[0]) } expect(parsed.success).toBeTruthy() } diff --git a/packages/nmea-parser/tests/protocols.test.ts b/packages/nmea-parser/tests/protocols.test.ts index 3fcc01a..4806293 100644 --- a/packages/nmea-parser/tests/protocols.test.ts +++ b/packages/nmea-parser/tests/protocols.test.ts @@ -1,5 +1,4 @@ import path from 'node:path' -import * as v from 'valibot' import { describe, test, expect } from 'vitest' import { Protocol, StoredSentence } from '../src/types' import { getStoreSentences, readProtocolsFile, readProtocolsString } from '../src/protocols' @@ -208,8 +207,8 @@ describe('Protocols Files', () => { test('Right protocols file', () => { const { protocols } = readProtocolsFile(PROTOCOLS_FILE) protocols.forEach(protocol => { - const parsed = v.safeParse(ProtocolSchema, protocol) - if (!parsed.success) { console.error(parsed.issues[0].message) } + const parsed = ProtocolSchema.safeParse(protocol) + if (!parsed.success) { console.error((parsed.errors as string[])[0]) } expect(parsed.success).toBeTruthy() }) // expect(protocols).toStrictEqual(EXPECTED_PROTOCOLS) diff --git a/packages/nmea-parser/tests/schemas.test.ts b/packages/nmea-parser/tests/schemas.test.ts index 3ed19dc..f3d91bc 100644 --- a/packages/nmea-parser/tests/schemas.test.ts +++ b/packages/nmea-parser/tests/schemas.test.ts @@ -1,14 +1,13 @@ import { describe, test, expect } from 'vitest' -import * as v from 'valibot' import { VersionSchema } from '../src/schemas' describe('Version Schema', () => { test('Proper versions', () => { - ['3.2.1', '3.2', '3'].forEach(version => expect(v.parse(VersionSchema, version)).toStrictEqual(version)) + ['3.2.1', '3.2', '3'].forEach(version => expect(VersionSchema.parse(version)).toStrictEqual(version)) }) test('Failure versions', () => { - ['3.2.1.0', '3.a', 'asdfa', '-1', '3.-2'].forEach(version => expect(v.safeParse(VersionSchema, version).success).toBeFalsy()) + ['3.2.1.0', '3.a', 'asdfa', '-1', '3.-2'].forEach(version => expect(VersionSchema.is(version)).toBeFalsy()) }) }) \ No newline at end of file diff --git a/packages/nmea-parser/tests/sentences.test.ts b/packages/nmea-parser/tests/sentences.test.ts index becc857..9448b75 100644 --- a/packages/nmea-parser/tests/sentences.test.ts +++ b/packages/nmea-parser/tests/sentences.test.ts @@ -1,9 +1,7 @@ import { describe, expect, test } from 'vitest' -import * as v from 'valibot' -import { UnsignedIntegerSchema, Int16Schema, Int32Schema, Int8Schema, IntegerSchema, Uint16Schema, Uint32Schema, Uint8Schema} from '@schemasjs/valibot-numbers' import { generateSentenceFromModel, getNMEAUnparsedSentence, getNumberValue, getUnknownSentence, getValue } from '../src/sentences' import { FieldType, NMEAPreParsed, NMEAUnparsedSentence, StoredSentence } from '../src/types' -import { NMEALikeSchema, NMEAPreParsedSentenceSchema } from '../src/schemas' +import { Int16Schema, Int32Schema, Int8Schema, IntegerSchema, NMEALikeSchema, NMEAPreParsedSentenceSchema, Uint16Schema, Uint32Schema, Uint8Schema, UnsignedIntegerSchema } from '../src/schemas' import { CHECKSUM_LENGTH, DELIMITER_LENGTH, END_FLAG_LENGTH, SEPARATOR } from '../src/constants' describe('getNumberValue', () => { @@ -15,7 +13,7 @@ describe('getNumberValue', () => { test('uint8', () => { ['uint8', 'char'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.parse(Uint8Schema, value) + const expected = Uint8Schema.parse(value) expect(value).toBe(expected) }) }) @@ -23,7 +21,7 @@ describe('getNumberValue', () => { test('uint16', () => { ['uint16', 'unsigned short'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.parse(Uint16Schema, value) + const expected = Uint16Schema.parse(value) expect(value).toBe(expected) }) }) @@ -31,7 +29,7 @@ describe('getNumberValue', () => { test('uint32', () => { ['uint32', 'unsigned int'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.parse(Uint32Schema, value) + const expected = Uint32Schema.parse(value) expect(value).toBe(expected) }) }) @@ -47,7 +45,7 @@ describe('getNumberValue', () => { test('int8', () => { ['int8', 'signed char'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.parse(Int8Schema, value) + const expected = Int8Schema.parse(value) expect(value).toBe(expected) }) }) @@ -55,7 +53,7 @@ describe('getNumberValue', () => { test('int16', () => { ['int16', 'short'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.parse(Int16Schema, value) + const expected = Int16Schema.parse(value) expect(value).toBe(expected) }) }) @@ -63,7 +61,7 @@ describe('getNumberValue', () => { test('int32', () => { ['int32', 'int'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.parse(Int32Schema, value) + const expected = Int32Schema.parse(value) expect(value).toBe(expected) }) }) @@ -79,8 +77,8 @@ describe('getNumberValue', () => { test('float32', () => { ['float32', 'float'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.safeParse(IntegerSchema, value) - if (expected.success) { console.log(`Value = ${value} | Parsed = ${expected.output}`)} + const expected = IntegerSchema.safeParse(value) + if (expected.success) { console.log(`Value = ${value} | Parsed = ${expected.data}`)} expect(expected.success).toBeFalsy() }) }) @@ -88,8 +86,8 @@ describe('getNumberValue', () => { test('float64', () => { ['float64', 'double'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.safeParse(IntegerSchema, value) - if (expected.success) { console.log(`Value = ${value} | Parsed = ${expected.output}`)} + const expected = IntegerSchema.safeParse(value) + if (expected.success) { console.log(`Value = ${value} | Parsed = ${expected.data}`)} expect(expected.success).toBeFalsy() }) }) @@ -111,22 +109,22 @@ describe('getValue', () => { test('numbers -> unsigned integers', () => { ['uint8', 'uint16', 'uint32'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.parse(UnsignedIntegerSchema, value) + const expected = UnsignedIntegerSchema.parse(value) expect(value).toBe(expected) }); }) test('numbers -> integers', () => { ['int8', 'int16', 'int32'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.parse(IntegerSchema, value) + const expected = IntegerSchema.parse(value) expect(value).toBe(expected) }); }) test('numbers -> floats', () => { ['float32', 'float64'].forEach(type => { const value = getNumberValue(type as FieldType) - const expected = v.safeParse(IntegerSchema, value) - if (expected.success) { console.log(`Value = ${value} | Parsed = ${expected.output}`)} + const expected = IntegerSchema.safeParse(value) + if (expected.success) { console.log(`Value = ${value} | Parsed = ${expected.data}`)} expect(expected.success).toBeFalsy() }) }) @@ -161,7 +159,7 @@ describe('generateSentenceFromModel', () => { } test('Happy path', () => { const expected = generateSentenceFromModel(testSentence) - const parsed = v.parse(NMEALikeSchema, expected) + const parsed = NMEALikeSchema.parse(expected) expect(parsed).toBe(expected) const info = parsed.slice(1, - (DELIMITER_LENGTH + CHECKSUM_LENGTH + END_FLAG_LENGTH)).split(SEPARATOR) @@ -223,7 +221,7 @@ describe('unknown sentence', () => { const text = generateSentenceFromModel(testSentence) const unparsedSentence: NMEAUnparsedSentence = getNMEAUnparsedSentence(text) as NMEAUnparsedSentence const preparsedFrame = { timestamp: Date.now(), ...unparsedSentence } - const input: NMEAPreParsed = v.parse(NMEAPreParsedSentenceSchema, preparsedFrame) + const input: NMEAPreParsed = NMEAPreParsedSentenceSchema.parse(preparsedFrame) const result = getUnknownSentence(input) expect(result.protocol.name).toBe('UNKNOWN') expect(result.raw).toBe(text)