{"version":3,"file":"string.mjs","names":[],"sources":["../../src/schemas/string.ts"],"sourcesContent":["import { BaseSchema } from \"../core/base\";\nimport type { CombinedStandardProps, SchemaPrimitive } from \"../core/types\";\n\nexport class StringSchemaType extends BaseSchema<unknown, string> {\n  readonly type: SchemaPrimitive = \"string\";\n  private _validateDate = false;\n  private _validateUUID = false;\n  private _validateRegex = false;\n  private _validateEmail = false;\n  private _validatePhone = false;\n  private _validateDomain = false;\n  private _requireHttpOrHttps = false;\n  private _minLength?: number;\n  private _maxLength?: number;\n\n  constructor() {\n    super();\n    this.jsonSchema = { type: \"string\" };\n  }\n\n  primitive(): SchemaPrimitive {\n    return this.type;\n  }\n\n  /**\n   * Require minimum string length.\n   * @param {number} n Minimum length.\n   * @returns {StringSchemaType} New constrained schema.\n   */\n  minLength(n: number): StringSchemaType {\n    const schema = new StringSchemaType();\n    Object.assign(schema, this);\n    schema._minLength = n;\n    schema.jsonSchema = { ...this.jsonSchema, minLength: n };\n    return schema;\n  }\n\n  /**\n   * Require maximum string length.\n   * @param {number} n Maximum length.\n   * @returns {StringSchemaType} New constrained schema.\n   */\n  maxLength(n: number): StringSchemaType {\n    const schema = new StringSchemaType();\n    Object.assign(schema, this);\n    schema._maxLength = n;\n    schema.jsonSchema = { ...this.jsonSchema, maxLength: n };\n    return schema;\n  }\n\n  /**\n   * Validate value as parseable date string.\n   * @returns {StringSchemaType} New constrained schema.\n   */\n  date(): StringSchemaType {\n    const schema = new StringSchemaType();\n    Object.assign(schema, this);\n    schema._validateDate = true;\n    schema.jsonSchema = { ...this.jsonSchema, format: \"date\" };\n    return schema;\n  }\n\n  /**\n   * Validate value as a UUID string.\n   * @returns {StringSchemaType} New constrained schema.\n   */\n  uuid(): StringSchemaType {\n    const schema = new StringSchemaType();\n    Object.assign(schema, this);\n    schema._validateUUID = true;\n    schema.jsonSchema = { ...this.jsonSchema, format: \"uuid\" };\n    return schema;\n  }\n\n  /**\n   * Validate value matches a regex.\n   * @param {RegExp} regex Pattern to match.\n   * @returns {StringSchemaType} New constrained schema.\n   */\n  regex(regex: RegExp): StringSchemaType {\n    const schema = new StringSchemaType();\n    Object.assign(schema, this);\n    schema._validateRegex = true;\n    schema.jsonSchema = { ...this.jsonSchema, pattern: regex.source };\n    return schema;\n  }\n\n  /**\n   * Validate value as an email.\n   * @returns {StringSchemaType} New constrained schema.\n   */\n  email(): StringSchemaType {\n    const schema = new StringSchemaType();\n    Object.assign(schema, this);\n    schema._validateEmail = true;\n    schema.jsonSchema = { ...this.jsonSchema, format: \"email\" };\n    return schema;\n  }\n\n  /**\n   * Validate value as a phone number.\n   * @returns {StringSchemaType} New constrained schema.\n   */\n  phone(): StringSchemaType {\n    const schema = new StringSchemaType();\n    Object.assign(schema, this);\n    schema._validatePhone = true;\n    schema.jsonSchema = { ...this.jsonSchema, format: \"phone\" };\n    return schema;\n  }\n\n  /**\n   * Validate value as a domain name.\n   * @param {boolean} [requireHttpOrHttps=true] Require http(s) prefix.\n   * @returns {StringSchemaType} New constrained schema.\n   */\n  domain(requireHttpOrHttps = true): StringSchemaType {\n    const schema = new StringSchemaType();\n    Object.assign(schema, this);\n    schema._validateDomain = true;\n    schema._requireHttpOrHttps = requireHttpOrHttps;\n    schema.jsonSchema = { ...this.jsonSchema, format: \"domain\" };\n    return schema;\n  }\n\n  get [\"~standard\"](): CombinedStandardProps<unknown, string> {\n    return {\n      version: 1,\n      vendor: \"h-schema\",\n      jsonSchema: {\n        input: () => this.jsonSchema,\n        output: () => this.jsonSchema,\n      },\n      validate: (value: unknown) => {\n        if (this._coerce && typeof value !== \"string\") {\n          value = String(value);\n        }\n\n        if (typeof value !== \"string\") {\n          return { issues: [{ message: `Expected string, received ${typeof value}` }] };\n        }\n\n        if (this._minLength !== undefined && value.length < this._minLength) {\n          return { issues: [{ message: `String shorter than ${this._minLength}` }] };\n        }\n        if (this._maxLength !== undefined && value.length > this._maxLength) {\n          return { issues: [{ message: `String longer than ${this._maxLength}` }] };\n        }\n        if (this._validateUUID && !this._isValidUUID(value)) {\n          return { issues: [{ message: \"Invalid UUID format\" }] };\n        }\n        if (this._validateRegex && !this._isValidRegex(value)) {\n          return { issues: [{ message: \"Invalid regex format\" }] };\n        }\n        if (this._validateEmail && !this._isValidEmail(value)) {\n          return { issues: [{ message: \"Invalid email format\" }] };\n        }\n        if (this._validatePhone && !this._isValidPhone(value)) {\n          return { issues: [{ message: \"Invalid phone number format\" }] };\n        }\n        if (this._validateDomain && !this._isValidDomain(value)) {\n          return { issues: [{ message: \"Invalid domain format\" }] };\n        }\n        if (this._validateDate && !this._isValidDate(value)) {\n          return { issues: [{ message: \"Invalid date format\" }] };\n        }\n\n        return { value };\n      },\n      types: {\n        input: {} as unknown,\n        output: {} as string,\n      },\n    };\n  }\n\n  private _isValidDate(value: string): boolean {\n    const date = new Date(value);\n    return !Number.isNaN(date.getTime());\n  }\n\n  private _isValidUUID(value: string): boolean {\n    const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;\n    return uuidRegex.test(value);\n  }\n\n  private _isValidRegex(value: string): boolean {\n    return new RegExp(this.jsonSchema.pattern!).test(value);\n  }\n\n  private _isValidEmail(value: string): boolean {\n    const emailRegex = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/;\n    return emailRegex.test(value);\n  }\n\n  private _isValidPhone(value: string): boolean {\n    const phoneRegex = /^\\+?[0-9]{7,15}$/;\n    return phoneRegex.test(value);\n  }\n\n  private _isValidDomain(value: string): boolean {\n    let domainRegex = /^[a-z0-9]+([-.]{1}[a-z0-9]+)*\\.[a-z]{2,6}$/;\n    if (this._requireHttpOrHttps) {\n      domainRegex = /^https?:\\/\\/[a-z0-9]+([-.]{1}[a-z0-9]+)*\\.[a-z]{2,6}$/;\n    }\n    return domainRegex.test(value);\n  }\n}\n"],"mappings":";;AAGA,IAAa,mBAAb,MAAa,yBAAyB,WAA4B;CAChE,OAAiC;CACjC,gBAAwB;CACxB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