Skip to content

feature: Add String Type Support Using Regex and Standard Built-in Functions #158

@hospotho

Description

@hospotho

Describe the feature

Basic regex support and built-in string functions should be relatively easy to integrate into validate, parsing and serialization function.
Using the email validation example from your video, adding regex support would only require one additional line:

const $$User = {
  validate(input: unknown): input is User {
    return (
      typeof input === 'object' &&
      input !== null &&
      'id' in input &&
      typeof input.id === 'string' &&
      'name' in input &&
      typeof input.name === 'string' &&
      'email' in input &&
      typeof input.email === 'string' &&
      // add regex test
      /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(input.email)
    );
  },
  parse,
  serialize,
};

I haven’t gone through the entire ts-schema/src/compiler/, but it seems feasible to do so. Maybe some refactoring on a.string() (or just setter proxy for a.object?) and add some templates for regex and built-in string functions.

Additional context

Would you be willing to implement this feature?
Yes, but I may need some time to familiarize myself with the codebase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions