You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Specs] More advanced API specification format #15
The format itself is decent but I think that it could go even further.
I feel like if the params field would be reworked, using the JSON API docs and their different versions as data source for various modules would be really easy.
Some use cases I had in mind:
building a "debug" module that validates parameter types / formats and outputs very verbose run-time errors if the specified input doesn't follow the spec.
writing dynamic tests for the Node.js core itself that fuzzes the native methods and detects if errors are correctly thrown in case of invalid parameters
custom error handling policies where the end-user (developer) can choose to handle all different types of errors through callbacks rather than exceptions (for invalid inputs).
Where notEmpty and isPath are validator functions that could possibly be added to the utils core module.
I don't think the change itself would be huge nor time consuming after the initial coverage of the current API's.
Since this doesn't involve writing C/C++ code, I'd be willing to work actively on this feature !
Obviously, this could be done by an exterior NPM module but in my opinion a feature like a detailed spec / spec validation should be centralized in the core repository.
writing dynamic tests for the Node.js core itself that fuzzes the native methods and detects if errors are correctly thrown in case of invalid parameters
That's not a bad idea! Although the test suite should cover the API surface pretty well, more validation won't hurt.
I do see a practical issue though: there are a fair number of API methods that allow more inputs than what is documented for backwards compatibility reasons. I'm not sure how many exactly but I imagine you could end up with a fuzzer that's a big pile of special cases.
Backwards compatibility could be partly handled by versioning the specs in relation to the Core and then flagging the special cases as being linked to a particular past version.
@Mickael-van-der-Beek - I like it. Another potential use case that a lot of people on the node group really wanted was an alphabetical API reference like Angular, et al, rather than just the man-page style. Last time I checked, the json docs didn't have quite enough consistency to build an API reference automatically.
@darrenderidder - Thanks! An alphabetical spec would definitely be a cool improvement!
Currently the JSON and HTML API specs are generated with a non-NPM-published module called node-doc-generator developed by @isaacs.
I feel that although it's very convenient, I'd prefer to do it the other way around.
e.g: JSON API specs generate HTML and maybe also let description strings be markdown that is than converted to HTML.
Here is an example of the schema syntax I have in mind:
The type key, when not used in a signature description, can take three possible values: class, module and method.
The deprecated key is an optional flag that displays a visual warning to the user reading through the specs to underline that it is not recommended to specify this parameter.
It supports type and format validation like above but also various options that can be configured at the module level or spec level:
typeStrict (default=true): validation should fail in case the type doesn't match the spec
formatStrict (default=true): validation should fail in case the format doesn't match the spec
existenceStrict (default=true): validation should fail in case a parameter is not specified
(like a global { optional: true })
nullAsExistence (default=false): null is considered as an existing (as in not missing) value
undefinedAsExistence (default=false): undefined is considered as an existing (as in not missing) value
If you want more details about the spec format for nested Objects / Arrays or wildcard Array element validation, PM me or look at the tests for node-cerberus.
I'm sorry, I didn't have the time to write documentation yet.
The fuzzer module, in the same repo is still a work in progress but I already created a directory containing a list of "special" values for the Node.js + JavaScript native type, that will be used as payloads during fuzzing tests. Link below:
Which means that a format could be specified as a String (representing a validation method name) or an Object containing a name key for the method name and an Array of args that will be applied to the validator.
The 'ref:fileEncoding' argument here would be the fileEncoding variable value at validation time.
It's a bit of a hack so I'm definitely open other ideas !
Activity
bnoordhuis commentedon Nov 3, 2014
That's not a bad idea! Although the test suite should cover the API surface pretty well, more validation won't hurt.
I do see a practical issue though: there are a fair number of API methods that allow more inputs than what is documented for backwards compatibility reasons. I'm not sure how many exactly but I imagine you could end up with a fuzzer that's a big pile of special cases.
Mickael-van-der-Beek commentedon Nov 3, 2014
Here's an example in MongoDB's native Node.js driver docs:
http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find
darrenderidder commentedon Nov 16, 2014
@Mickael-van-der-Beek - I like it. Another potential use case that a lot of people on the node group really wanted was an alphabetical API reference like Angular, et al, rather than just the man-page style. Last time I checked, the json docs didn't have quite enough consistency to build an API reference automatically.
Refactoring of the module. The validator still needs improvement and …
Mickael-van-der-Beek commentedon Nov 18, 2014
@darrenderidder - Thanks! An alphabetical spec would definitely be a cool improvement!
Currently the JSON and HTML API specs are generated with a non-NPM-published module called
node-doc-generatordeveloped by @isaacs.I feel that although it's very convenient, I'd prefer to do it the other way around.
e.g: JSON API specs generate HTML and maybe also let description strings be markdown that is than converted to HTML.
Here is an example of the schema syntax I have in mind:
The
typekey, when not used in a signature description, can take three possible values:class,moduleandmethod.The
deprecatedkey is an optional flag that displays a visual warning to the user reading through the specs to underline that it is not recommended to specify this parameter.I have already written and tested (86% coverage) a spec
validator modulein a new repo I created calledNode Cerberus: Mickael-van-der-Beek/node-cerberus@864f93a.It supports type and format validation like above but also various options that can be configured at the module level or spec level:
typeStrict(default=true): validation should fail in case the type doesn't match the specformatStrict(default=true): validation should fail in case the format doesn't match the specexistenceStrict(default=true): validation should fail in case a parameter is not specified(like a global
{ optional: true })nullAsExistence(default=false):nullis considered as an existing (as in not missing) valueundefinedAsExistence(default=false):undefinedis considered as an existing (as in not missing) valueIf you want more details about the spec format for nested
Objects /Arrays or wildcardArrayelement validation, PM me or look at the tests fornode-cerberus.I'm sorry, I didn't have the time to write documentation yet.
The
fuzzer module, in the same repo is still a work in progress but I already created a directory containing a list of "special" values for the Node.js + JavaScript native type, that will be used as payloads during fuzzing tests. Link below:Mickael-van-der-Beek/node-cerberus/src/fuzzer/payloads
What do you guys think ?
PS: One case I'm currently not handling in my validation spec format is linked parameters. e.g:
function MyMethod (fileData, fileEncoding)where
fileDatais expected to be aStringencoded using thefileEncodingencoding.One possible solution for this:
Which means that a
formatcould be specified as aString(representing a validation method name) or anObjectcontaining anamekey for the method name and anArrayof args that will be applied to the validator.The
'ref:fileEncoding'argument here would be thefileEncodingvariable value at validation time.It's a bit of a hack so I'm definitely open other ideas !