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

TypeScript: passing schema via variable doesn't work #173

Open
bsnote opened this issue Sep 20, 2018 · 1 comment
Open

TypeScript: passing schema via variable doesn't work #173

bsnote opened this issue Sep 20, 2018 · 1 comment

Comments

@bsnote
Copy link

bsnote commented Sep 20, 2018

TypeScript fails to compile the following snippet:

import createValidator = require('is-my-json-valid');

const schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'number' },
  },
  required: [
    'name'
  ]
};

createValidator(schema);

showing the following error:

[ts]
Argument of type '{ type: string; properties: { name: { type: string; }; age: { type: string; }; }; required: string[]; }' is not assignable to parameter of type 'AnySchema'.
  Type '{ type: string; properties: { name: { type: string; }; age: { type: string; }; }; required: string[]; }' is not assignable to type 'AnyOneOfSchema'.
    Property 'oneOf' is missing in type '{ type: string; properties: { name: { type: string; }; age: { type: string; }; }; required: string[]; }'.
@LinusU
Copy link
Collaborator

LinusU commented Sep 20, 2018

Yeah, I wrote down some thoughts about that here: #170 (comment)

Unfortunately, this is not something that we can support right now, because of limitations in TypeScript. You can work around it by explicitly narrowing your types:

import createValidator = require('is-my-json-valid');

const schema = {
  type: 'object' as 'object',
  properties: {
    name: { type: 'string' as 'string' },
    age: { type: 'number' as 'number' },
  },
  required: [
    'name' as 'name'
  ]
};

createValidator(schema);

Feel free to contribute to the discussion here on how to best solve this in TypeScript: microsoft/TypeScript#26332

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

2 participants