schema-utils icon indicating copy to clipboard operation
schema-utils copied to clipboard

Add Schema type declaration

Open oliverfindl opened this issue 5 years ago • 11 comments

  • Operating System: Ubuntu 16.04 LTS
  • Node Version: 14.15.3
  • NPM Version: 6.14.10
  • webpack Version: ^4
  • schema-utils Version: ^3

Feature Proposal

Please add standalone Schema type declaration.

Currently is possible to import it manually from validate method type declarations, but it is not convenient:

import * as schemaUtils from "schema-utils";
import { Schema } from "schema-utils/declarations/validate";

Feature Use Case

To use typed schema object, that is passed into validate method.

oliverfindl avatar Dec 30 '20 10:12 oliverfindl

types auto generated by typescript

alexander-akait avatar Dec 30 '20 12:12 alexander-akait

Hello,

I see. Are there any plans for standalone types support, e.g. using DefinitelyTyped same way as loader-utils?

My current project using schema-utils looks like:

import * as schemaUtils from "schema-utils";
import { Schema, ValidationErrorConfiguration as SchemaValidationErrorConfiguration } from "schema-utils/declarations/validate";

// copied from: https://github.com/webpack/schema-utils/blob/master/declarations/validate.d.ts#L35
// this could / should be defined as its own type and be imported as above and therefore preventing breaking changes in future
type SchemaOptions = Array<object> | object;

const OPTIONS_SCHEMA: Schema = { /* ... */ };

/* ... */

const options: SchemaOptions & LoaderOptions = Object.assign({}, DEFAULT_OPTIONS, loaderOptions);
const schemaConfiguration: SchemaValidationErrorConfiguration = { name: PACKAGE_NAME, baseDataPath: "options" };

schemaUtils.validate(OPTIONS_SCHEMA, options, schemaConfiguration);

Which is kinda cumberstone and imported / defined types are not namespaced in schemaUtils, so they are polluting global namespace.

Thanks.

oliverfindl avatar Dec 30 '20 13:12 oliverfindl

Are there any plans for standalone types support, e.g. using DefinitelyTyped same way as loader-utils?

We already provide types

Which is kinda cumberstone and imported / defined types are not namespaced in schemaUtils, so they are polluting global namespace.

Where?

Using:

import { Schema, ValidationErrorConfiguration as SchemaValidationErrorConfiguration } from "schema-utils/declarations/validate";

is normal, it is part of public API

Again, we don't write types, it generates by typescript

alexander-akait avatar Dec 30 '20 13:12 alexander-akait

Hello,

Are there any plans for standalone types support, e.g. using DefinitelyTyped same way as loader-utils?

We already provide types

Provided types are in different declaration file instead of index.d.ts which is loaded together with index.js. Therefore its required to import them manually, which pollutes global namespace (this can be kinda solved by prefixing / renaming them in import statement) instead of having them in one single variable.

Which is kinda cumberstone and imported / defined types are not namespaced in schemaUtils, so they are polluting global namespace.

Where?

In provided example.

Using:

import { Schema, ValidationErrorConfiguration as SchemaValidationErrorConfiguration } from "schema-utils/declarations/validate";

is normal, it is part of public API

Same response as first one.

Again, we don't write types, it generates by typescript

I understand and I don't want to argue, so if there are no plans for this, then feel free to close this issue.

Thanks.

oliverfindl avatar Dec 30 '20 14:12 oliverfindl

Why do not install json-schema? WE don't know what is schema will be used, you can use 4/6/7/etc version, I think it is part of your code

alexander-akait avatar Dec 30 '20 14:12 alexander-akait

Hello,

why use another package, if you already got defined possible Schema type in declarations/validate.d.ts#L11 (I understand its auto-generated by tsc and therefore you can't modify it). Not to mention, your Schema type might change in future and it won't be fully compatible anymore, so I prefer to have it included in same package. And as I iterated my code, types for rest of input arguments for validate method should be included too.

I'm not familiar with generating types from javascript files, therefore I could not suggest solution different than write declarations manually.

Thanks.

oliverfindl avatar Dec 30 '20 14:12 oliverfindl

In theory we can export Schema, but yes, it can be changed, so it is why not exported

alexander-akait avatar Dec 30 '20 14:12 alexander-akait

Hello,

this was my initial idea. If you include Schema type, it will be always up-to-date to used version of schema-utils. If I defined Schema type myself, in future it could be different to what schema-utils is expecting (although, not very likely to happen).

Cleanest solution from my perspective would be using same approach as loader-utils as I suggested here. Unfortunately, it requires write declarations manually.

We can let this issue open for now, so more devs can suggest different / better solution.

Thanks.

oliverfindl avatar Dec 30 '20 14:12 oliverfindl