UNPKG

@mitre-attack/attack-data-model

Version:

A TypeScript API for the MITRE ATT&CK data model

39 lines (37 loc) 1.23 kB
// src/schemas/common/stix-timestamp.ts import { z } from "zod"; var RFC3339_REGEX = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d+)?)Z$/; var StixTimestampError = { InvalidFormat: { code: z.ZodIssueCode.custom, message: "Invalid STIX timestamp format: must be an RFC3339 timestamp with a timezone specification of 'Z'." } }; var isValidRFC3339 = (timestamp) => { if (!RFC3339_REGEX.test(timestamp)) return false; const date = new Date(timestamp); return !isNaN(date.getTime()); }; var stixTimestampSchema = z.custom( (val) => { if (val instanceof Date) { return isValidRFC3339(val.toISOString()); } if (typeof val === "string") { return isValidRFC3339(val); } return false; }, { message: StixTimestampError.InvalidFormat.message } ).describe( "Represents timestamps across the CTI specifications. The format is an RFC3339 timestamp, with a required timezone specification of 'Z'." ); var stixCreatedTimestampSchema = stixTimestampSchema.brand("StixCreatedTimestamp"); var stixModifiedTimestampSchema = stixTimestampSchema.brand("StixModifiedTimestamp"); export { stixTimestampSchema, stixCreatedTimestampSchema, stixModifiedTimestampSchema };