Proposal to add symbol: "hasInstanceStrict"

Александр Ефремов mr.efrem at gmail.com
Tue Feb 20 08:54:52 UTC 2018


I use `ajv` (https://github.com/epoberezkin/ajv <https://github.com/epoberezkin/ajv>) for checking input payloads in the microservices. But it’s enough verbose and also I don’t want to load it in the browser. My offer a more simply and quicker in use.

> 20 февр. 2018 г., в 12:34, kai zhu <kaizhu256 at gmail.com> написал(а):
> 
>> 
>> On Feb 20, 2018, at 10:05 AM, Aleksander Efremov <mr.efrem at gmail.com <mailto:mr.efrem at gmail.com>> wrote:
>> 
>> And still one question about `function decorators` . They will be work if in function parameters uses destructuring?
>> 
>> ```
>> @rttc(PrimitiveNumber, PrimitiveString, PrimitiveBoolean)
>> function a (b, { c, d }) {}
>> ```
> 
> @aleksander, javascript was not designed to validate functions and class-instances passed around inside a silo'd nodejs-process.  its designed to solve UX-problems, which requires validating data passed across multiple javascript-processes (browser <-> server <-> database).  you are only painting yourself in a corner, when you’re eventually asked by your employer to apply your nodejs validation-code to sanitize user-inputs from the browser, which you can’t because its not possible (or very difficult) to serialize functions and class-instances over tcp/udp communication.
> 
> the employer paying your salary would greatly appreciate it, if you created a validation-system that works not just inside a silo'd nodejs-process, but also in higher-level integrated-systems that includes browsers, storage-engines, and other nodejs-servers talking to each other via tcp/udp.
> 
> for these higher-level integrated-systems, your have several serialization-protocols to choose from:
> 1. json
> 2. xml
> 3. protobuf
> 
> however, in a javascript-only system, json will always be the obvious choice. i've worked with both xml and protobufs in nodejs, and they are both more trouble than they’re worth.  for xml, why deal with extra 3rd-party serializers and parsers you are never sure will be as reliable as builtin JSON.parse and JSON.stringify?  for protobufs, the serializers and parsers are even more unreliable (from my experience with node-grpc). they are also slower than JSON.parse and JSON.stringify due to how expensive it is to make function-calls to c++ native-code inside nodejs, and protobuf-data is not human-readable/debuggable.
> 
> going with json, there are several validators you can choose from.  i'm most familiar with swagger (v2.0), so will provide you with a standalone real-world swagger-solution to your
> ```js
> @rttc(PrimitiveNumber, PrimitiveString, PrimitiveBoolean)
> function a (b, { c, d }) {}
> ```
> validation-problem.  (with attached screenshots showing it working in both nodejs and browser)
> 
> <Screen-Shot-2018-02-20-at-3.25.45-PM-compressor.png>
> 
> browser-example from https://kaizhu256.github.io/node-swgg-github-all/build..beta..travis-ci.org/app/ <https://kaizhu256.github.io/node-swgg-github-all/build..beta..travis-ci.org/app/>
> <Screen-Shot-2018-02-20-at-3.03.45-PM-compressor.png>
> 
> ```javascript
> /*
>  * validate.js
>  *
>  * to run code in nodejs, you will need to
>  * $ npm install swgg
>  */
> 
> /*jslint
>     bitwise: true,
>     browser: true,
>     maxerr: 8,
>     maxlen: 100,
>     node: true,
>     nomen: true,
>     regexp: true,
>     stupid: true
> */
> 
> 'use strict’;
> 
> function aa(myData) {
> /*
>  * this standalone (browser/nodejs compatible) function will validate myData with the given format
>  * {
>  *     bb: <number>,
>  *     mySubData: {
>  *         cc: <string>,
>  *         dd: <boolean>
>  *     }
>  * }
>  */
>     var myResult, swgg;
>     swgg = (typeof window === 'object' && window)
>         ? window.swgg
>         : require('swgg');
>     try {
>         swgg.validateBySwaggerSchema({
>             data: myData,
>             prefix: ['function aa', 'myData'],
>             schema: { $ref: '#/definitions/mySchema' },
>             swaggerJson: {
>                 definitions: {
>                     mySchema: {
>                         properties: {
>                             bb: { type: 'number' },
>                             mySubData: { $ref: '#/definitions/mySubSchema' }
>                         },
>                         required: ['bb', 'mySubData']
>                     },
>                     mySubSchema: {
>                         properties: {
>                             cc: { type: 'string' },
>                             dd: { type: 'boolean' }
>                         },
>                         required: ['cc', 'dd']
>                     }
>                 }
>             }
>         });
>     } catch (errorCaught) {
>         if (typeof window === 'object' && window) {
>             console.error(errorCaught.message);
>         } else {
>             console.error('\u001b[33m' + errorCaught.message + '\u001b[39m');
>         }
>         return;
>     }
>     // process myData after vaidation
>     myResult = null;
>     if (myData.mySubData.dd) {
>         myResult = myData.bb + ' ' + myData.mySubData.cc <http://mydata.mysubdata.cc/>;
>     }
>     console.log(myResult);
> }
> 
> // test validaton failed
> aa({ bb: 'invalid number' });
> aa({ bb: 1234, mySubData: 'invalid object' });
> aa({ bb: 1234, mySubData: { cc: 'hello', dd: 'invalid boolean' } });
> aa({}); // missing bb
> aa({ bb: 1234 }); // missing mySubData
> aa({ bb: 1234, mySubData: {} }); // missing mySubData.cc <http://mysubdata.cc/>
> aa({ bb: 1234, mySubData: { cc: 'hello' } }); // missing mySubData.dd
> 
> // test validation passed
> aa({ bb: 1234, mySubData: { cc: 'hello', dd: true } });
> 
> /* output
> error.objectRequired - object function aa["myData"] = {"bb":"invalid number"} must have property "mySubData"
> error.itemType - value function aa["myData"]["mySubData"] = "invalid object" is not a valid object
> error.itemType - value function aa["myData"]["mySubData"]["dd"] = "invalid boolean" is not a valid boolean
> error.objectRequired - object function aa["myData"] = {} must have property "bb"
> error.objectRequired - object function aa["myData"] = {"bb":1234} must have property "mySubData"
> error.objectRequired - object function aa["myData"]["mySubData"] = {} must have property "cc"
> error.objectRequired - object function aa["myData"]["mySubData"] = {"cc":"hello"} must have property "dd"
> */
> 
> ```
> 
> 
>> 
>> 20 февр. 2018 г. 0:13 пользователь "mr.efrem" <mr.efrem at gmail.com <mailto:mr.efrem at gmail.com>> написал:
>> And still one question about `function decorators` . They will be work if in function parameters uses destructuring?
>> ```
>> @rttc(PrimitiveNumber, PrimitiveString, PrimitiveBoolean)
>> function a (b, { c, d }) {} 
>> ```
>> 
>> 
>> 
>> 
>> 
>> 
>> -------- Исходное сообщение --------
>> От: "T.J. Crowder" <tj.crowder at farsightsoftware.com <mailto:tj.crowder at farsightsoftware.com>>
>> Время: пн 19/2 20:58
>> Кому: Aleksander Efremov <mr.efrem at gmail.com <mailto:mr.efrem at gmail.com>>
>> Копия: es-discuss <es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>>
>> Тема: Re: Proposal to add symbol: "hasInstanceStrict"
>> 
>> On Mon, Feb 19, 2018 at 4:50 PM, Aleksander Efremov <mr.efrem at gmail.com <mailto:mr.efrem at gmail.com>> wrote:
>> > In my first example you can found what I mean.
>> 
>> Yes, I looked at your first example (and every other example in the thread) before replying. As it had nothing to do with `fetch`, but you specifically mentioned `fetch` in your message, I assumed you were asking something about `fetch`. If you were talking about the `const` in `const c: PrimitiveNumber = sum(1, 2);`, why say "`fetch`"?
>> 
>> -- T.J. Crowder
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
>> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180220/7ad629ab/attachment-0001.html>


More information about the es-discuss mailing list