小编Que*_*n3r的帖子

configService.get<T>('key') 在类型转换和 joi 验证上失败

我为 NestJs REST API 创建了一个自定义配置文件。这是应用程序正在侦听的端口的简单示例。

我有一个包含内容的 .env 文件

SERVER_PORT = 3000
Run Code Online (Sandbox Code Playgroud)

我的配置文件的示例

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import * as Joi from '@hapi/joi';

@Injectable()
export class ServerConfigService {
  constructor(private readonly configService: ConfigService) {
    const { error } = Joi.object({
      port: Joi.number()
        .port()
        .required(),
    }).validate({ port: this.port });

    if (error) { // no error thrown
      throw error;
    }

    console.log(typeof this.port); // type is string but should be number
  }

  public get port(): number { …
Run Code Online (Sandbox Code Playgroud)

typescript joi nestjs

1
推荐指数
1
解决办法
2652
查看次数

将 v-text-field 重置为空文本而不触发“不得为空”规则

我有一个基本的输入文本字段,想在提交后重置它。重置它时,我目前将其 v-model 更新为空字符串。不幸的是,这会触发我的输入规则并呈现错误消息。

代码示例:

<div id="app">
    <v-app>
        <v-text-field v-model="text" :rules="[value => !!value || 'Required']"></v-text-field>
        <v-btn @click="text = ''">submit and reset</v-btn>
    </v-app>
</div>

new Vue({
    el: '#app',
    vuetify: new Vuetify(),
    data() {
        return {
            text: ""
        };
    }
})
Run Code Online (Sandbox Code Playgroud)

有没有办法可以在不触发规则的情况下清空该字段?我的意思是它的行为是正确的,我希望如此,但是当我单击按钮时,我想将该字段重置为其初始状态。

javascript vue.js vuetify.js

1
推荐指数
1
解决办法
2040
查看次数

验证“真实”浮点字段的数字精度

我想验证坐标。这些将被存储为real数据列,来自Postgres 文档

实数 4 字节可变精度,不精确的 6 位小数精度

我从以下开始

@IsNumber()
@Min(-90)
@Max(90)
/* precision check */
public latitude: number;

@IsNumber()
@Min(-180)
@Max(180)
/* precision check */
public longitude: number;
Run Code Online (Sandbox Code Playgroud)

并想知道如何检查这些浮点数的精度。它必须具有 6 位精度。

提前致谢

typescript class-validator

1
推荐指数
1
解决办法
5629
查看次数

如何对扩展抽象类读取环境变量的类进行单元测试

我想要进行单元测试,并为我想要测试的 Nest API 提供一些配置服务。启动应用程序时,我使用 joi 包验证环境变量。

我有多个数据库、服务器的配置服务……所以我首先创建了一个基本服务。它能够读取环境变量,将原始字符串解析为所需的数据类型并验证该值。

import { ConfigService } from '@nestjs/config';
import { AnySchema, ValidationResult, ValidationError } from '@hapi/joi';

export abstract class BaseConfigurationService {
    constructor(protected readonly configService: ConfigService) {}

    protected constructValue(key: string, validator: AnySchema): string {
        const rawValue: string = this.configService.get(key);

        this.validateValue(rawValue, validator, key);

        return rawValue;
    }

    protected constructAndParseValue<TResult>(key: string, validator: AnySchema, parser: (value: string) => TResult): TResult {
        const rawValue: string = this.configService.get(key);
        const parsedValue: TResult = parser(rawValue);

        this.validateValue(parsedValue, validator, key);

        return parsedValue;
    }

    private validateValue<TValue>(value: TValue, …
Run Code Online (Sandbox Code Playgroud)

unit-testing node.js jestjs nestjs

1
推荐指数
1
解决办法
3503
查看次数

来回移动GameObject

我得到了一个我想要移动到A点的物体,当它到达A点时它应该移动到B点.当它到达B点时它应该移回A点.

我以为我可以使用Vector3.Lerp

void Update()
{
  transform.position = Vector3.Lerp(pointA, pointB, speed * Time.deltaTime);
}
Run Code Online (Sandbox Code Playgroud)

但是我怎么能回去呢?是否有一种优雅的方式来实现这一目标?显然我需要像这样的2 Lerps:

void Update()
{
  transform.position = Vector3.Lerp(pointA, pointB, speed * Time.deltaTime); // Move up
  transform.position = Vector3.Lerp(pointB, pointA, speed * Time.deltaTime); // Move down
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮帮我吗?

c# unity-game-engine

0
推荐指数
1
解决办法
2056
查看次数

设置宽度为50%时div不居中

当我将 div 居中时,它没有居中。看起来,他的左角就在屏幕的中间。如何减去宽度的一半,使 div 正确居中?

在我的 css 中我写的left: 50%是结果:

在此输入图像描述

有没有可能使这个 div 正确居中?

html javascript css jquery

0
推荐指数
1
解决办法
4623
查看次数

在Select上的C#Linq语法中创建新对象

我有一个Vector2对象列表.我想从每个元素中选择一个值并对这些值进行排序.在那之后,我想获得最低价值.

Vector2 cheapestCellPosition = openCells.Select(x => new {
        Vector2 = x,
        Value = GetCostToTarget(x, targetPosition) + GetCell(x).Cost.GetValueOrDefault()
    })
    .OrderBy(x => x.Value)
    .First();
Run Code Online (Sandbox Code Playgroud)

此代码抛出错误

CS0029 C#无法隐式转换匿名类型:Sym.Vector2 Vector2,int值为Sym.Vector2

我怎样才能解决这个问题?我需要根据当前元素设置Value属性.

c# linq

0
推荐指数
1
解决办法
155
查看次数

从jsonwebtoken提取到期日期时间

据我所知,要使令牌无效,这是存储令牌的最佳方法,它是数据库的到期日期时间。要验证它,您只需从数据库中选择它,如果它存在,就知道它已失效。此外,您可以在数据库的过期日期时间之前删除每个过期的令牌。

因此,我创建了一个中间件,该中间件从授权标头中提取令牌,并将该令牌和到期日期时间附加到该request对象。signOut路由使令牌失效需要日期时间。

  async use(req: any, res: Response, next: NextFunction) {
    try {
      const headers: IncomingHttpHeaders = req.headers;
      const authorization: string = headers.authorization;
      const bearerToken: string[] = authorization.split(' ');
      const token: string = bearerToken[1];

      if (await this.authenticationsRepository.findByEncodedToken(token)) { // invalidated token?
        throw new Error(); // jump to catch
      }

      req.tokenPayload = verifyToken(token); // calls jwt.verify with secret
      next();
    } catch (error) {
      throw new UnauthorizedException();
    }
  }
Run Code Online (Sandbox Code Playgroud)

但是,如何exp从令牌中提取属性以计算到期日期时间呢?

javascript node.js jwt nestjs

0
推荐指数
1
解决办法
59
查看次数