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

issue with schema : 'impossible to assign type' #2

Open
bcotteret opened this issue Jul 25, 2024 · 2 comments
Open

issue with schema : 'impossible to assign type' #2

bcotteret opened this issue Jul 25, 2024 · 2 comments

Comments

@bcotteret
Copy link

bcotteret commented Jul 25, 2024

Hello,
I'm trying to implement a schema for my settings in my electron application
The app can lauch in dev, but fails to bundle in production

npm run dev  ## works
npm run build:linux  ## fails

Firstly, I've tried with you example

import { Conf } from "electron-conf/main";
const schema = {
  type: "object",
  properties: {
    locale: {
      type: "string",
      maxLength: 2,
      nullable: false,
      default: "fr"
    },
    access_token: {
      type: "string",
      nullable: true
    },
    user: {
      type: "object"
    },
    barcodePrinter: {
      type: "string",
      nullable: true
    },
    textPrinter: {
      type: "string",
      nullable: true
    }
  }
}
const conf = new Conf({ schema })
export { conf };

But then I got a Typescript issue

src/main/settings.ts:40:25 - error TS2322: Type '{ type: string; properties: { locale: { type: string; maxLength: number; nullable: boolean; default: string; }; access_token: { type: string; nullable: boolean; }; user: { type: string; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type 'JSONSchema<Record<string, unknown>> | undefined'.
  Type '{ type: string; properties: { locale: { type: string; maxLength: number; nullable: boolean; default: string; }; access_token: { type: string; nullable: boolean; }; user: { type: string; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<Record<string, unknown>, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; }) | ({ ...; } & { ...; })'.
    Type '{ type: string; properties: { locale: { type: string; maxLength: number; nullable: boolean; default: string; }; access_token: { type: string; nullable: boolean; }; user: { type: string; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '{ oneOf: readonly UncheckedJSONSchemaType<Record<string, unknown>, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; }'.
      Property 'oneOf' is missing in type '{ type: string; properties: { locale: { type: string; maxLength: number; nullable: boolean; default: string; }; access_token: { type: string; nullable: boolean; }; user: { type: string; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' but required in type '{ oneOf: readonly UncheckedJSONSchemaType<Record<string, unknown>, false>[]; }'.

40 const conf = new Conf({ schema })
                           ~~~~~~

  node_modules/ajv/dist/types/json-schema.d.ts:25:5
    25     oneOf: readonly UncheckedJSONSchemaType<T, IsPartial>[];
           ~~~~~
    'oneOf' is declared here.


Found 1 error in src/main/settings.ts:40

I also tried this : (using ajv to enforce type)
I understood ajv is an underlying piece in schema validation
So I took their example

import { JSONSchemaType } from 'ajv'
import { Conf } from "electron-conf/main";

interface Settings {
  locale: string,
  access_token: string,
  user: object,
  barcodePrinter: string,
  textPrinter: string
}

const schema: JSONSchemaType<Settings> = {
  type: "object",
  properties: {
    locale: {
      type: "string",
      maxLength: 2,
      nullable: false,
      default: "fr"
    },
    access_token: {
      type: "string",
      nullable: true
    },
    user: {
      type: "object"
    },
    barcodePrinter: {
      type: "string",
      nullable: true
    },
    textPrinter: {
      type: "string",
      nullable: true
    }
  }
}
const conf = new Conf({ schema })
export { conf };

Then I got this error,

src/main/settings.ts:12:7 - error TS2322: Type '{ type: "object"; properties: { locale: { type: "string"; maxLength: number; nullable: false; default: string; }; access_token: { type: "string"; nullable: true; }; user: { type: "object"; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type 'JSONSchemaType<Settings>'.
  Type '{ type: "object"; properties: { locale: { type: "string"; maxLength: number; nullable: false; default: string; }; access_token: { type: "string"; nullable: true; }; user: { type: "object"; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<Settings, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; }) | ({ ...; } & { ...; }) | ({ ...; } & ... 2 more ... & { ...; })'.
    Type '{ type: "object"; properties: { locale: { type: "string"; maxLength: number; nullable: false; default: string; }; access_token: { type: "string"; nullable: true; }; user: { type: "object"; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'.
      Type '{ type: "object"; properties: { locale: { type: "string"; maxLength: number; nullable: false; default: string; }; access_token: { type: "string"; nullable: true; }; user: { type: "object"; }; barcodePrinter: { ...; }; textPrinter: { ...; }; }; }' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.
        The types of 'properties.access_token' are incompatible between these types.
          Type '{ type: "string"; nullable: true; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; })'.
            Type '{ type: "string"; nullable: true; }' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<string, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<...>> | undefined; definitions?: Record<...> | undefined; } & { ...; }) | ({ ...; } & ... 1 more ... & { ...; }) | ({ ...; } &...'.
              Type '{ type: "string"; nullable: true; }' is not assignable to type '{ type: "string"; } & StringKeywords & { allOf?: readonly UncheckedPartialSchema<string>[] | undefined; anyOf?: readonly UncheckedPartialSchema<string>[] | undefined; ... 4 more ...; not?: UncheckedPartialSchema<...> | undefined; } & { ...; } & { ...; }'.
                Type '{ type: "string"; nullable: true; }' is not assignable to type '{ nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; }'.
                  Types of property 'nullable' are incompatible.
                    Type 'true' is not assignable to type 'false'.

12 const schema: JSONSchemaType<Settings> = {
         ~~~~~~

Any idea or hint ?
Or can I by pass this TS problem ?

Thanks

@bcotteret bcotteret changed the title issue with schema : 'impossible to assing type' issue with schema : 'impossible to assign type' Jul 25, 2024
@alex8088
Copy link
Owner

Use required to specify a non-nullable properties.

import { Conf, type JSONSchema } from "electron-conf/main"

const schema: JSONSchema<Settings> = {
  type: "object",
    properties: {},
   required: ['user']
}

@theores
Copy link

theores commented Aug 14, 2024

同样的问题

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

No branches or pull requests

3 participants