ansman / validate.js

A declarative validation library written javascript
https://validatejs.org
MIT License
2.63k stars 336 forks source link

TypeScript definitions #209

Closed wayneashleyberry closed 6 years ago

wayneashleyberry commented 7 years ago

I'm not sure if this is an issue with TypeScript itself, or the definitions for validate.js.

I've put together an example repo at https://github.com/wayneashleyberry/validate-types/

The following sample code fails a typescript check.

const validate = require("validate.js");

validate.validators.inclusion.message = "is an invalid value";

validate.validators.string = function(value, options, key, attributes) {
    return validate.isString(value) === true ? null : "must be a string";
};

validate.formatters.custom = function(errors) {
    return errors.map(function(error) {
        return error.validator;
    });
};

const constraints = {
    username: {
        presence: true,
        exclusion: {
            within: ["nicklas"],
            message: "'%{value}' is not allowed"
        }
    },
    password: {
        presence: true,
        length: {
            minimum: 6,
            message: "must be at least 6 characters"
        }
    }
};

const errors = validate({ password: "bad" }, constraints, {
    format: "custom"
});

console.log(errors);
❯ yarn test
yarn test v0.23.4
$ tsc --allowJs --checkJs --noEmit *.js
test.js(4,10): error TS2339: Property 'validators' does not exist on type 'typeof "/Users/wayne/src/github.com/wayneashleyberry/validate-types/node_modules/validate.js/vali...'.
test.js(6,10): error TS2339: Property 'validators' does not exist on type 'typeof "/Users/wayne/src/github.com/wayneashleyberry/validate-types/node_modules/validate.js/vali...'.
test.js(7,18): error TS2339: Property 'isString' does not exist on type 'typeof "/Users/wayne/src/github.com/wayneashleyberry/validate-types/node_modules/validate.js/vali...'.
test.js(10,10): error TS2339: Property 'formatters' does not exist on type 'typeof "/Users/wayne/src/github.com/wayneashleyberry/validate-types/node_modules/validate.js/vali...'.
test.js(33,16): error TS2349: Cannot invoke an expression whose type lacks a call signature. Type 'typeof "/Users/wayne/src/github.com/wayneashleyberry/validate-types/node_modules/validate.js/vali...' has no compatible call signatures.
error Command failed with exit code 1.
rgilling commented 7 years ago

I'm also having trouble getting validate.js to working in a Typescript environment. I don't think the Typescript bindings are right for this library. I can't get the compiler to recognise these as properties even thought they're mentioned in the documentation:

I've also raised an open question on Stack Overflow: http://stackoverflow.com/questions/43928659/resolving-various-typescript-type-declaration-issues

egorshulga commented 7 years ago

I've added this code to my project and validate.validators is no longer not found.

// validate.d.ts
import { ValidateJS } from 'validate.js';
declare module 'validate.js' {
  interface ValidateJS {
    validators: any;
  }
}

It would be great if typescript definition supported entire library functionality.

Pritilender commented 7 years ago

The validate.d.ts is good, but it seams that npm version lacks proper typings. In the same file from npm you can see that there is only

export declare interface ValidateJS {
  (attributes: any, constraints: any, options?: any): any;
  async(attributes: any, constraints: any, options?: any): Promise<any>;
  single(value: any, constraints: any, options?: any): any;
}

declare var validate: ValidateJS;

export default validate;

@ansman can you please update npm version to match git one?

cspotcode commented 7 years ago

I found a typo in the declarations: https://github.com/ansman/validate.js/blob/master/validate.d.ts#L9

It should be wrapErrors not WrapErrors

I was in the middle of writing an updated declaration file before realizing you already have one here (doh!) so I can merge my work into this one.

ansman commented 7 years ago

Does this issue still exist in 0.12.0?

Pritilender commented 7 years ago

@ansman it looks fine in 0.12.0. Thanks 👍