-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interested in validators? #128
Comments
assertion guards are a relatively new feature (TS 3.7), and while I feel that it is aligned with these JSON types, I would not want to impose breaking changes on consumers that would only be solvable with a compiler upgrade.
I think that this means that consumers would be ok to compile with older TS versions (i.e., 2.8 is the minimum required for If you can implement this such that existing use with TS 2.8 can continue as-is, I'm 100% open to your proposal, even if it means that TS 3.7 is required in order to use the validators. I can assist with writing tests such that this required condition is maintained and protected from future regression. |
Btw looks like this library currently works with TS >= 2.3. I'd be fine with a minimum requirement of 2.8, but would not want to jump ahead of the |
For what it's worth, you can make validators without using import type * as JSONAPI from "jsonapi-typescript";
// SHALLOW checks for various top-level JSON:API types
// Note that `type` is treated as optional.
const is_plain_object = (x: any) =>
x !== null && typeof x === "object" && !Array.isArray(x);
const is_resource = (x: any) => is_plain_object(x) && typeof x.id === "string";
const is_primary_data = (x: any): x is JSONAPI.PrimaryData =>
is_resource(x) || (Array.isArray(x) && x.every(item => is_resource(item)));
export const is_jsonapi_data_document = (x: any): x is JSONAPI.DocWithData =>
is_primary_data(x?.data);
export const is_jsonapi_meta_document = (x: any): x is JSONAPI.DocWithMeta =>
is_plain_object(x?.meta);
export const is_jsonapi_error_document = (x: any): x is JSONAPI.DocWithErrors =>
Array.isArray(x?.errors);
export const is_jsonapi_document = (x: any): x is JSONAPI.Document =>
is_jsonapi_data_document(x) ||
is_jsonapi_meta_document(x) ||
is_jsonapi_error_document(x); You can then use this with type narrowing as documented. if (is_jsonapi_document(body)) {
// `body` is JSONAPI.Document here
} else {
// `body` is whatever it was, possibly with JSONAPI.Document subtracted
} |
We have written validation code that follows the exact structure of these types, using the new typescript
asserts
feature. It's great for safely casting documents that came over the network into the types from this package.Since the validators need to match exactly with how the types are defined, I think it would be appropriate to upstream the validators into this package. Do you agree?
If so I can make a PR adding the validators. They would live in a separate module inside this package, so they wouldn't add anything to apps that don't choose to import from
jsonapi-typescript/validators
.The text was updated successfully, but these errors were encountered: