Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export type 'Format' #432

Merged
merged 1 commit into from Jun 23, 2022
Merged

feat: export type 'Format' #432

merged 1 commit into from Jun 23, 2022

Conversation

matthiashermsen
Copy link
Contributor

When adding a custom format via

const customFormat = { ... };
ajvInstance.addFormat('customFormat', customFormat);

the variable customFormat must be of type Format. It would be nice to import this type from validate-value, otherwise one must install ajv in addition.

@goloroden
Copy link
Member

Hey @matthiashermsen 👋

Thanks for bringing up this PR 😊

I'm not sure if I got this correctly: If you want to register a custom format, AFAICS you need a custom ajv instance, so you must install and import ajv by yourself anyway. Am I missing something?

@matthiashermsen
Copy link
Contributor Author

matthiashermsen commented Jun 22, 2022

@goloroden Hey :)

It is possible to get a custom ajv instance from validate-value ( getDefaultAjvInstance() )

Given the following sample code

import { getDefaultAjvInstance, Parser } from 'validate-value';

const createParser = function (): Parser<T> {
  const ajvInstance = getDefaultAjvInstance();

  const myCustomFormat = {
    type: 'string',
    validate: (candidate: string) => !!candidate
  };

  ajvInstance.addFormat('myCustomFormat', myCustomFormat);

  return new Parser<T>(schema(), { ajvInstance });
};

This won't work, because TS can't infer myCustomFormat to Format which is required for the addFormat function. You will get the error

Argument of type '{ type: string; validate: (candidate: string) => boolean; }' is not assignable to parameter of type 'Format'.
Property 'async' is missing in type '{ type: string; validate: (candidate: string) => boolean; }' but required in type 'AsyncFormatDefinition'.ts(2345)

It is possible to change the code to

import { getDefaultAjvInstance, Parser } from 'validate-value';

const createParser = function (): Parser<T> {
  const ajvInstance = getDefaultAjvInstance();

  ajvInstance.addFormat('myCustomFormat', {
    type: 'string',
    validate: (candidate: string) => !!candidate
  });

  return new Parser<T>(schema(), { ajvInstance });
};

but in my case, it should import the Format from another file and unfortunately I can't import this type from validate-value.

import { Format } from 'validate-value'; // Module '"validate-value"' has no exported member 'Format'

const myCustomFormat = { // It doesn't know that this should be of type `Format`
  type: 'string',
  validate: (candidate: string) => !!candidate
};

For now I solved this by installing ajv but I think it is better to depend only on validate-value ( the packages might be out of sync
or validate-value might use different technologies in the future )

@goloroden
Copy link
Member

Makes sense 👍

@goloroden goloroden merged commit 16ec76b into thenativeweb:main Jun 23, 2022
goloroden pushed a commit that referenced this pull request Jun 23, 2022
# [9.4.0](9.3.3...9.4.0) (2022-06-23)

### Features

* export type 'Format' ([#432](#432)) ([16ec76b](16ec76b))
@matthiashermsen matthiashermsen deleted the export-format-type branch June 23, 2022 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants