JS: Why type validation need to be required ?

Quentin LAFFONT

Full Stack JS Developer

 

https://qlaffont.com

           

            @qlaffont

Pbl: Javascript is not type safe

When you are working with javascript, is not easy to know type :

 

- typeof x

- Array.isArray(x)

- isNaN(x)

Pbl: Javascript is strange !

Brian Leroux - WTFJS (DotJS)

Pbl: Security/Integrity issues

Example where database return error

Solution: Typescript !

Javascript + Type Checking = Typescript

API: TS is it enough ?

TS doesn't check on runtime !

Solution: Use TS + Type Checker

- AJV (Eslint, Indeed, Salesforce ...) -> JSON Validator

  - Weekly Downloads: 30 023 955

   - Use in Fastify Framework


- Joi (Brave, Beat Musique, Vrbo ...) -> JS Validator

  - Weekly Downloads: 4 233 791

   - Use in Hapi Framework

 

- check-types.js -> JS Validator

  - Weekly Downloads: 3 042 559

12 May 2020

AJV Example

Joi Example

Recommandation: AJV

- Most used library

90 contributors 🏗 | 5,000 dependent npm packages ⚙️ |7,000 github stars ⭐️
5,000,000 dependent repositories 🚀 |120,000,000 npm downloads per month! 💯

 

- Base on JSON Schema Standard -> http://json-schema.org

 

- Use JSON format

  - Can be use on every project : Browser / Mobile / Server

  - Can be use on every language who implement this standard

    Example : Golang Package -> gojsonschema

Recommandation: AJV

Library Mean Latency Total in Seconds
AJV 0.6 ms 6.21s
JOI 3.3 ms 33.22 s

Performance Test

10000 requests

Macbook 2019 I7 2.6Ghz / 16go DDR4 |Node v10.16.3
Report :
https://raw.githubusercontent.com/qlaffont/ts-is-not-the-complete-answer/master/perf-tests/perf-test.report.txt

Questions ?

Javascript: Why type validation need to be required ?

By Quentin LAFFONT

Javascript: Why type validation need to be required ?

Presentation about why we need to check type in JS (TS + Type Check Lib)

  • 894