-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(common): Add json schema const decorator.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {Const, JsonSchema} from "../../../src/jsonschema"; | ||
import {stubSchemaDecorator} from "./utils"; | ||
|
||
describe("Const", () => { | ||
describe("when const is a string", () => { | ||
it("should store data", () => { | ||
const decorateStub = stubSchemaDecorator(); | ||
const schema = new JsonSchema(); | ||
Const("0"); | ||
// @ts-ignore | ||
decorateStub.getCall(0).args[0](schema); | ||
schema.const.should.eq("0"); | ||
decorateStub.restore(); | ||
}); | ||
}); | ||
|
||
describe("when const is a boolean", () => { | ||
it("should store data", () => { | ||
const decorateStub = stubSchemaDecorator(); | ||
const schema = new JsonSchema(); | ||
Const(false); | ||
// @ts-ignore | ||
decorateStub.getCall(0).args[0](schema); | ||
schema.const.should.eq(false); | ||
decorateStub.restore(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {JSONSchema6Type} from "json-schema"; | ||
import {decoratorSchemaFactory} from "../utils/decoratorSchemaFactory"; | ||
|
||
/** | ||
* The const keyword is used to restrict a value to a fixed value. | ||
* | ||
* | ||
* ## Example | ||
* ### With a string | ||
* | ||
* ```typescript | ||
* class Model { | ||
* @Const("value1") | ||
* property: "value1"; | ||
* } | ||
* ``` | ||
* | ||
* Will produce: | ||
* | ||
* ```json | ||
* { | ||
* "type": "object", | ||
* "properties": { | ||
* "property": { | ||
* "type": "string", | ||
* "const": "value1" | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* * ### With a boolean | ||
* | ||
* ```typescript | ||
* class Model { | ||
* @Const(true) | ||
* property: boolean; | ||
* } | ||
* ``` | ||
* | ||
* Will produce: | ||
* | ||
* ```json | ||
* { | ||
* "type": "object", | ||
* "properties": { | ||
* "property": { | ||
* "type": "boolean", | ||
* "const": true | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param {string | number | boolean } constValue | ||
* @returns {Function} | ||
* @decorator | ||
* @ajv | ||
* @property | ||
* @jsonschema | ||
* @auto-map The data will be stored on the right place according to the type and collectionType (primitive or collection). | ||
*/ | ||
export function Const(constValue: JSONSchema6Type | any) { | ||
return decoratorSchemaFactory(schema => { | ||
schema.mapper.const = constValue; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters