Skip to content

Infer type schema #512

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

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
68ff81c
Improved performance for const schema
DanieleFedeli Aug 16, 2022
dbd3395
Merge branch 'master' of github.com:DanieleFedeli/fast-json-stringify
DanieleFedeli Aug 16, 2022
5fa6e25
Improve const object schema performance
DanieleFedeli Aug 16, 2022
ca55edf
Improve string and object performance
DanieleFedeli Aug 16, 2022
b2ce4d5
Remove unwanted validation for 'const' properties
DanieleFedeli Aug 25, 2022
05c92cf
Restored example file
DanieleFedeli Aug 25, 2022
f73da5a
Changed findIndex with indexOf
DanieleFedeli Aug 25, 2022
e2c3bd6
Removed useless inference when schema is const
DanieleFedeli Aug 25, 2022
ca1357d
Review requests
DanieleFedeli Aug 26, 2022
844eba5
Rebase to #510 (use hasOwnProperty from Object.prototype)
DanieleFedeli Aug 26, 2022
49301dc
Use isRequired when available
DanieleFedeli Aug 26, 2022
7dd683e
use hasOwnProperty from prototype (#510)
Uzlopak Aug 23, 2022
f193e7f
Added type inference
DanieleFedeli Aug 26, 2022
b2ec98f
Test cases with tsd
DanieleFedeli Aug 26, 2022
59395a3
Formatted package.json
DanieleFedeli Aug 26, 2022
e8fb32c
Changed indentation style in typescript files to match the project one
DanieleFedeli Aug 26, 2022
0eb362a
Restored FJS format style
DanieleFedeli Aug 27, 2022
b80bc4a
Restore FJS format style
DanieleFedeli Aug 27, 2022
3879db9
Added support for nullable / type: [..., 'null']
DanieleFedeli Aug 28, 2022
2531220
Allow manual inference
DanieleFedeli Aug 28, 2022
2aec5be
Added type inference
DanieleFedeli Aug 26, 2022
e3eb5ea
Test cases with tsd
DanieleFedeli Aug 26, 2022
cea1f45
Formatted package.json
DanieleFedeli Aug 26, 2022
51a7701
Changed indentation style in typescript files to match the project one
DanieleFedeli Aug 26, 2022
368828f
Allow manual inference
DanieleFedeli Aug 28, 2022
988da22
Merge branch 'master' of https://github.com/fastify/fast-json-stringi…
DanieleFedeli Aug 28, 2022
6d36fd4
Merge branch 'fastify-master' into infer-type-schema
DanieleFedeli Aug 28, 2022
eb9da30
Merge branch 'infer-type-schema' of github.com:DanieleFedeli/fast-jso…
DanieleFedeli Aug 28, 2022
30a6c33
Revert "Align Fork"
DanieleFedeli Aug 28, 2022
17fb105
Merge pull request #2 from DanieleFedeli/revert-1-master
DanieleFedeli Aug 28, 2022
d721d0e
Merge pull request #3 from DanieleFedeli/master
DanieleFedeli Aug 29, 2022
9d5d765
Indented test file with space
DanieleFedeli Aug 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import Ajv, { Options as AjvOptions } from "ajv"
import {FromSchema, JSONSchema} from 'json-schema-to-ts'

declare namespace build {
interface BaseSchema {
/**
Expand Down Expand Up @@ -189,13 +191,14 @@ interface StandaloneOption extends build.Options {
declare function build(schema: build.AnySchema, options: DebugOption): { code: string, ajv: Ajv };
declare function build(schema: build.AnySchema, options: DeprecateDebugOption): { code: string, ajv: Ajv };
declare function build(schema: build.AnySchema, options: StandaloneOption): string;
declare function build(schema: build.AnySchema, options?: build.Options): (doc: any) => any;
declare function build(schema: build.StringSchema, options?: build.Options): (doc: string) => string;
declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): (doc: number) => string;
declare function build(schema: build.NullSchema, options?: build.Options): (doc: null) => "null";
declare function build(schema: build.BooleanSchema, options?: build.Options): (doc: boolean) => string;
declare function build(schema: build.ArraySchema | build.TupleSchema, options?: build.Options): (doc: any[]) => string;
declare function build(schema: build.ObjectSchema, options?: build.Options): (doc: object) => string;
declare function build(schema: build.Schema, options?: build.Options): (doc: object | any[] | string | number | boolean | null) => string;
declare function build(schema: build.AnySchema, options?: build.Options): <TDoc = any>(doc: TDoc) => any;
declare function build(schema: build.StringSchema, options?: build.Options): <TDoc = string>(doc: TDoc) => string;
declare function build(schema: build.IntegerSchema | build.NumberSchema, options?: build.Options): <TDoc = number>(doc: TDoc) => string;
declare function build(schema: build.NullSchema, options?: build.Options): <TDoc = null>(doc: TDoc) => "null";
declare function build(schema: build.BooleanSchema, options?: build.Options): <TDoc = boolean>(doc: boolean) => string;
declare function build(schema: build.ArraySchema | build.TupleSchema, options?: build.Options): <TDoc = any[]>(doc: TDoc) => string;
declare function build(schema: build.ObjectSchema, options?: build.Options): <TDoc = object>(doc: TDoc) => string;
declare function build(schema: build.Schema, options?: build.Options): <TDoc = object | any[] | string | number | boolean | null> (doc: TDoc) => string;
declare function build<TSchema extends JSONSchema>(schema: TSchema, options?: build.Options): <TDoc extends FromSchema<TSchema> = FromSchema<TSchema>>(doc: TDoc) => any;

export = build;
12 changes: 1 addition & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -906,17 +906,7 @@ function buildValue (location, input) {
switch (type) {
case 'string': {
code += `
${statement}(
typeof ${input} === "string" ||
${input} === null ||
${input} instanceof RegExp ||
(
typeof ${input} === "object" &&
typeof ${input}.toString === "function" &&
${input}.toString !== Object.prototype.toString &&
!(${input} instanceof Date)
)
)
${statement}(${input} === null || typeof ${input} === "${type}" || ${input} instanceof RegExp || (typeof ${input} === "object" && Object.prototype.hasOwnProperty.call(${input}, "toString")))
${nestedResult}
`
break
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"benchmark": "node ./benchmark/bench-cmp-lib.js",
"lint:fix": "standard --fix",
"test:lint": "standard",
"test:typescript": "tsc --project ./test/types/tsconfig.json",
"test:typescript": "tsc --project ./test/types/tsconfig.json && tsd",
"test:unit": "tap -J test/*.test.js test/**/*.test.js",
"test": "npm run test:lint && npm run test:unit && npm run test:typescript"
},
Expand Down Expand Up @@ -45,6 +45,7 @@
"simple-git": "^3.7.1",
"standard": "^17.0.0",
"tap": "^16.0.1",
"tsd": "^0.22.0",
"typescript": "^4.0.2",
"webpack": "^5.40.0"
},
Expand All @@ -53,12 +54,16 @@
"ajv": "^8.10.0",
"ajv-formats": "^2.1.1",
"fast-uri": "^2.1.0",
"json-schema-to-ts": "^2.5.5",
"rfdc": "^1.2.0"
},
"standard": {
"ignore": [
"schema-validator.js"
]
},
"runkitExampleFilename": "example.js"
"runkitExampleFilename": "example.js",
"tsd": {
"directory": "test/types"
}
}
82 changes: 82 additions & 0 deletions test/types/schema-inference.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { expectError } from "tsd";
import build from "../..";

// Schema with constant value
const schema = {
type: "object",
properties: {
foo: {
const: "bar",
},
},
additionalProperties: false,
} as const;
const stringify = build(schema);

expectError(stringify({ foo: "baz" }));
expectError(stringify({ foo: 1 }));
expectError(stringify({ foo: null }));

stringify({ foo: "bar" });

// Schema with property multiple types
const schema1 = {
type: "object",
properties: {
foo: {
type: ["string", "integer", "null"],
},
},
} as const;
const stringify1 = build(schema1);
expectError(stringify1({ foo: true }));
stringify1({ foo: "bar" });
stringify1({ foo: "bar", anotherOne: null });
stringify1({ foo: 1 });
stringify1({ foo: null });

// Schema with nested properties
const schema2 = {
type: "object",
properties: {
foo: {
type: "object",
properties: {
bar: { type: "object", properties: { baz: { type: "string" } } },
},
required: ["bar"],
},
},
} as const;
const stringify2 = build(schema2);
expectError(
stringify2({
foo: {
bar: { baz: 1 },
},
})
);
expectError(
stringify2({
foo: {
bar: null,
},
})
);
stringify2({ foo: { bar: { baz: "baz" } } });
stringify2({ foo: { bar: {} } });

// With inference
interface Schema {
id: string;
a?: number;
}

const stringify3 = build({
type: "object",
properties: { a: { type: "string" } },
});
stringify3<Schema>({ id: "123" });
stringify3<Schema>({ a: 123, id: "123" });
expectError(stringify3<Schema>({ anotherOne: "bar" }));
expectError(stringify3<Schema>({ a: "bar" }));
29 changes: 0 additions & 29 deletions test/typesArray.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,35 +438,6 @@ test('object that is simultaneously a string and a json switched', (t) => {
t.equal(valueObj, '{"simultaneously":{"foo":"hello"}}')
})

test('class instance that is simultaneously a string and a json', (t) => {
t.plan(2)

const schema = {
type: 'object',
properties: {
simultaneously: {
type: ['string', 'object'],
properties: {
foo: { type: 'string' }
}
}
}
}

class Test {
toString () { return 'hello' }
}

const likeObjectId = new Test()

const stringify = build(schema)
const valueStr = stringify({ simultaneously: likeObjectId })
t.equal(valueStr, '{"simultaneously":"hello"}')

const valueObj = stringify({ simultaneously: { foo: likeObjectId } })
t.equal(valueObj, '{"simultaneously":{"foo":"hello"}}')
})

test('should throw an error when type is array and object is null', (t) => {
t.plan(1)
const schema = {
Expand Down