-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hxtree <[email protected]>
- Loading branch information
Showing
18 changed files
with
376 additions
and
61 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. | ||
{ | ||
"pnpmShrinkwrapHash": "b494a61a3dd66fffe41bf91e5d3f0d656e935872", | ||
"pnpmShrinkwrapHash": "374cc425860ef0a63a23f3ed83e8b649005cedfb", | ||
"preferredVersionsHash": "8ae0ba5bd02ec9c5763773a15e27aee08a6567f6" | ||
} |
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
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
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
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,32 @@ | ||
import { IsDiceNotation } from '@cats-cradle/validation-schemas'; | ||
import { getSchemas } from '../get-schemas'; | ||
|
||
describe('getSchemas', () => { | ||
let testClass: TestClass; | ||
|
||
class TestClass { | ||
@IsDiceNotation() | ||
public property: string; | ||
} | ||
|
||
beforeEach(async () => { | ||
testClass = new TestClass(); | ||
}); | ||
|
||
it('should get accurate pattern for schemas', async () => { | ||
const schemas = getSchemas(); | ||
|
||
expect(schemas).toMatchObject({ | ||
TestClass: { | ||
properties: { | ||
property: { | ||
pattern: /(\d+)?d(\d+)([+-]\d+)?/, | ||
type: 'string', | ||
}, | ||
}, | ||
type: 'object', | ||
required: ['property'], | ||
}, | ||
}); | ||
}); | ||
}); |
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
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
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,6 @@ | ||
import { validationMetadatasToSchemas } from 'class-validator-jsonschema'; | ||
import { SchemaObject } from 'openapi3-ts'; | ||
|
||
export function getSchemas(): Record<string, SchemaObject> { | ||
return validationMetadatasToSchemas(); | ||
} |
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
31 changes: 10 additions & 21 deletions
31
libraries/validation-schemas/src/custom/is-dice-notation.ts
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 |
---|---|---|
@@ -1,29 +1,18 @@ | ||
import { registerDecorator, ValidationOptions } from 'class-validator'; | ||
import { Matches, matches, ValidationOptions } from 'class-validator'; | ||
|
||
export function IsDiceNotationValidator(value: any): boolean { | ||
return typeof value === 'string' && /(\d+)?d(\d+)([\+\-]\d+)?/.test(value); | ||
export const DICE_NOTATION_REGEX = /(\d+)?d(\d+)([+-]\d+)?/; | ||
|
||
export function IsDiceNotationValidator(value: string) { | ||
return matches(value, DICE_NOTATION_REGEX); | ||
} | ||
|
||
/** | ||
* Checks if is dice notation | ||
* | ||
* "d4", "2d6", "4d8+2", "3d5+20*3" | ||
* | ||
* @param property | ||
* Checks if is dice notation, e.g. "d4", "2d6", "4d8+2", "3d5+20*3" | ||
* @param validationOptions | ||
* @returns | ||
*/ | ||
export function IsDiceNotation(validationOptions?: ValidationOptions) { | ||
return function (object: Object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'isDiceNotation', | ||
target: object.constructor, | ||
propertyName, | ||
constraints: [], | ||
options: validationOptions, | ||
validator: { | ||
validate: IsDiceNotationValidator, | ||
}, | ||
}); | ||
}; | ||
export function IsDiceNotation( | ||
validationOptions?: ValidationOptions, | ||
): PropertyDecorator { | ||
return Matches(DICE_NOTATION_REGEX, validationOptions); | ||
} |
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
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
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
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,20 @@ | ||
import { IsLongitude, IsLatitude } from '@cats-cradle/validation-schemas'; | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class QueryDto { | ||
@IsLatitude() | ||
@ApiProperty({ | ||
description: 'Latitude', | ||
default: '38.2942', | ||
type: String, | ||
}) | ||
latitude: string; | ||
|
||
@IsLongitude() | ||
@ApiProperty({ | ||
description: 'Longitude', | ||
default: '141.4164', | ||
type: String, | ||
}) | ||
longitude: string; | ||
} |
23 changes: 23 additions & 0 deletions
23
services/weather-control/src/module/weather/weather.controller.ts
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,23 @@ | ||
import { | ||
Controller, | ||
Post, | ||
Body, | ||
Res, | ||
Get, | ||
VERSION_NEUTRAL, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { WeatherService } from './weather.service'; | ||
|
||
@Controller({ path: 'weather', version: ['1', VERSION_NEUTRAL] }) | ||
export class WeatherController { | ||
constructor(private readonly weatherService: WeatherService) {} | ||
|
||
@Post() | ||
async fetch( | ||
@Query('latitude') latitude: number, | ||
@Query('longitude') longitude: number, | ||
) { | ||
return this.weatherService.get(latitude, longitude); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
services/weather-control/src/module/weather/weather.e2e-spec.ts
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,50 @@ | ||
import supertest from 'supertest'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { INestApplication, Injectable } from '@nestjs/common'; | ||
import { FakerFactory } from '@cats-cradle/faker-factory'; | ||
import { WeatherService } from './weather.service'; | ||
import { WeatherController } from './weather.controller'; | ||
import { ClimateType } from './climates.type'; | ||
import { QueryDto } from './query.dto'; | ||
|
||
describe('/weather', () => { | ||
let app: INestApplication; | ||
let weatherService: WeatherService; | ||
|
||
beforeAll(async () => { | ||
const moduleRef: TestingModule = await Test.createTestingModule({ | ||
imports: [], | ||
controllers: [WeatherController], | ||
providers: [WeatherService], | ||
}).compile(); | ||
|
||
app = moduleRef.createNestApplication(); | ||
|
||
weatherService = moduleRef.get<WeatherService>(WeatherService); | ||
|
||
await app.init(); | ||
}); | ||
|
||
afterAll(async () => { | ||
app.close(); | ||
}); | ||
|
||
describe('POST /weather', () => { | ||
it('should determine climate', async () => { | ||
const body = await FakerFactory.create<QueryDto>(QueryDto, { | ||
// longitude: '0', | ||
}); | ||
console.log(body); | ||
console.log(body); | ||
|
||
const response = await supertest(app.getHttpServer()) | ||
.post('/weather') | ||
.send(body) | ||
.expect(201); | ||
|
||
expect(response.body).toMatchObject({ | ||
climate: ClimateType.POLAR, | ||
}); | ||
}); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
services/weather-control/src/module/weather/weather.module.ts
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,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { WeatherController } from './weather.controller'; | ||
import { WeatherService } from './weather.service'; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [WeatherController], | ||
providers: [WeatherService], | ||
}) | ||
export class WeatherModule {} |