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

Missing type for Validator.validate(json, schema, options)

Open peteorpeter opened this issue 5 years ago • 3 comments

A type is missing for the overload of Validator.validate that is used for validating against subschemas. The method signature exists and is in the docs at: https://github.com/peteorpeter/z-schema#validate-against-subschema

There are two other overloads of that method that are typed, but not that one.

This means that Typescript users will have problems using that method signature. I worked around it by overriding all of the z-schema types locally and adding the missing type, a la https://stackoverflow.com/questions/41627631/exclude-overwrite-npm-provided-typings/41641001#41641001.

I'd be happy to submit a PR, though I'm not sure what object shapes should be allowed in that third parameter. For my uses, I limited it to containing a schemaPath field and no more:

  /**
   * @param json - either a JSON string or a parsed JSON object
   * @param schema - the JSON object representing the schema
   * @param options - an object with a schemaPath field, a string with the dot path of the relevant subschema
   * @returns true if json matches schema
   */
  validate(json: any, schema: any, options: { schemaPath: string }): boolean

peteorpeter avatar Dec 10 '19 00:12 peteorpeter

@peteorpeter happy to merge a PR.

Third parameter is this: https://github.com/zaggino/z-schema/blob/master/index.d.ts#L7-L29 so:

validate(json: any, schema: any, options: Validator.Options): boolean

schemaPath is missing from Validator.Options

zaggino avatar Dec 12 '19 15:12 zaggino

Another missing Validator method:

  .setRemoteReference(url: string, definition: any): void

I'll see if I can get a PR put together with these changes in the next week or two.

peteorpeter avatar Dec 13 '19 02:12 peteorpeter

I'm also not able to use certain features because of the missing method signature. Additionally @zaggino it seems like https://github.com/zaggino/z-schema/blob/master/index.d.ts#L7-L29 is also missing the includeErrors option? Is interface Options the sensible place to put it or does it only apply to the validate() method?

aahventures avatar Jul 03 '20 11:07 aahventures