newcat / baklavajs

Graph / node editor in the browser using VueJS
http://baklava.tech
MIT License
1.52k stars 113 forks source link

bug [v2] BaseNumericInterface min 0 #361

Closed erwanito12 closed 7 months ago

erwanito12 commented 8 months ago

export class BaseNumericInterface extends NodeInterface implements IValidator { public min?: number; public max?: number;

constructor(name: string, value: number, min?: number, max?: number) {
    super(name, value);
    this.min = min;
    this.max = max;
}

public validate(v: number) {
    return (!this.min || v >= this.min) && (!this.max || v <= this.max);
}

} When this.min is 0, the !this.min expression returns true, because in JavaScript, 0 is considered a false value. This means that the v >= this.min part is never evaluated when min is 0.

To correct this problem, you must explicitly check whether min and max are undefined rather than evaluating them as booleans.

public validate(v: number) { return (this.min === undefined || v >= this.min) && (this.max === undefined || v <= this.max); }

starker-xp commented 7 months ago

Do you use Js or Ts? If you use ts, you probably have the strictNullChecks option set to false.

erwanito12 commented 7 months ago

i use Ts, tsconfig.json: { "compilerOptions": { "target": "esnext", "module": "esnext", "strict": true, "jsx": "preserve", "moduleResolution": "node", "experimentalDecorators": true, "skipLibCheck": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "forceConsistentCasingInFileNames": true, "useDefineForClassFields": true, "baseUrl": ".", "noImplicitAny": true, "sourceMap": true, "allowJs": true, "types": [ "webpack-env" ], "paths": { "@/*": [ "src/*" ] }, "lib": [ "esnext", "dom", "dom.iterable", "scripthost" ] }, "include": [ "src/**/*.ts", "src/**/*.tsx", "src/**/*.vue", "tests/**/*.ts", "tests/**/*.tsx" ], "exclude": [ "node_modules" ] }

newcat commented 7 months ago

Fixed in v2.4.0