当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript z-schema.validate函数代码示例

本文整理汇总了TypeScript中z-schema.validate函数的典型用法代码示例。如果您正苦于以下问题:TypeScript validate函数的具体用法?TypeScript validate怎么用?TypeScript validate使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了validate函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: verify

  public async verify(tx: IBaseTransaction<DelegateAsset>, sender: AccountsModel): Promise<void> {
    if (tx.recipientId) {
      throw new Error('Invalid recipient');
    }

    if (tx.amount !== 0) {
      throw new Error('Invalid transaction amount');
    }

    if (sender.isDelegate) {
      throw new Error('Account is already a delegate');
    }

    if (!tx.asset || !tx.asset.delegate) {
      throw new Error('Invalid transaction asset');
    }

    if (!tx.asset.delegate.username) {
      throw new Error('Username is undefined');
    }

    if (tx.asset.delegate.username !== tx.asset.delegate.username.toLowerCase()) {
      throw new Error('Username must be lowercase');
    }

    const username = String(tx.asset.delegate.username).toLowerCase().trim();

    if (username === '') {
      throw new Error('Empty username');
    }

    if (username.length > 20) {
      throw new Error('Username is too long. Maximum is 20 characters');
    }

    if (this.schema.validate(tx.asset.delegate.username.toUpperCase(), { format: 'address' })) {
      throw new Error('Username can not be a potential address');
    }

    if (!this.schema.validate(tx.asset.delegate.username, { format: 'username' })) {
      throw new Error('Username can only contain alphanumeric characters with the exception of !@$&_.');
    }

    const account = await this.accountsModule.getAccount({ username });
    if (account) {
      throw new Error(`Username already exists: ${username}`);
    }
  }
开发者ID:RiseVision,项目名称:rise-node,代码行数:48,代码来源:delegate.ts

示例2: objectNormalize

  public objectNormalize(tx: IBaseTransaction<VoteAsset>): IBaseTransaction<VoteAsset> {
    const report = this.schema.validate(tx.asset, voteSchema);
    if (!report) {
      throw new Error(`Failed to validate vote schema: ${this.schema.getLastErrors()
        .map((err) => err.message).join(', ')}`);
    }

    return tx;
  }
开发者ID:RiseVision,项目名称:rise-node,代码行数:9,代码来源:vote.ts

示例3: assertValidSchema

export function assertValidSchema(schema: z_schema,
                                  objToValidate: any,
                                  schemaToValidate: { obj: any, opts?: { errorString?: string } }) {
  if (!schema.validate(objToValidate, schemaToValidate.obj)) {
    const errorMessage = (schemaToValidate.opts || {}).errorString ||
      `${schema.getLastError().details[0].path} - ${schema.getLastErrors()[0].message}`;
    throw new Error(errorMessage);
  }

}
开发者ID:RiseVision,项目名称:rise-node,代码行数:10,代码来源:schemavalidators.ts

示例4: objectNormalize

  public objectNormalize(tx: IBaseTransaction<DelegateAsset>): IBaseTransaction<DelegateAsset> {
    removeEmptyObjKeys(tx.asset.delegate);

    const report = this.schema.validate(tx.asset.delegate, delegateSchema);
    if (!report) {
      throw new Error(`Failed to validate delegate schema: ${this.schema.getLastErrors()
        .map((err) => err.message).join(', ')}`);
    }

    return tx;
  }
开发者ID:RiseVision,项目名称:rise-node,代码行数:11,代码来源:delegate.ts

示例5: assertValidVote

  private assertValidVote(vote: string) {
    if (typeof(vote) !== 'string') {
      throw new Error('Invalid vote type');
    }

    if (['-', '+'].indexOf(vote[0]) === -1) {
      throw new Error('Invalid vote format');
    }

    const pkey = vote.substring(1);
    if (!this.schema.validate(pkey, { format: 'publicKey' })) {
      throw new Error('Invalid vote publicKey');
    }
  }
开发者ID:RiseVision,项目名称:rise-node,代码行数:14,代码来源:vote.ts

示例6: validateAgainstSchemas

function validateAgainstSchemas(vlspec, done?) {
  var isVlValid = validator.validate(vlspec, vlSchema);
  var errors;

  if (!isVlValid) {
    errors = validator.getLastErrors();
    console.log(inspect(errors, { depth: 10, colors: true }));
  }
  assert.deepEqual(isVlValid, true);

  var vegaSpec = vl.compile(vlspec);

  var isVgValid = validator.validate(vegaSpec, vgSchema);

  if (!isVgValid) {
    errors = validator.getLastErrors();
    console.log(inspect(errors, { depth: 10, colors: true }));
  }
  assert.deepEqual(isVgValid, true);

  if (done) {
    done();
  }
}
开发者ID:MikeWickwar,项目名称:firstyeoman,代码行数:24,代码来源:examples.test.ts

示例7: verify

  public async verify(tx: IBaseTransaction<SecondSignatureAsset>, sender: AccountsModel): Promise<void> {
    if (!tx.asset || !tx.asset.signature) {
      throw new Error('Invalid transaction asset');
    }

    if (tx.recipientId) {
      throw new Error('Invalid recipient');
    }

    if (tx.amount !== 0) {
      throw new Error('Invalid transaction amount');
    }

    if (!tx.asset.signature.publicKey ||
      !this.schema.validate(tx.asset.signature.publicKey, { format: 'publicKey' })) {
      throw new Error('Invalid public key');
    }
  }
开发者ID:RiseVision,项目名称:rise-node,代码行数:18,代码来源:secondSignature.ts

示例8: use

  public use(request: express.Request, response: any, next: (err?: any) => any) {
    castFieldsToNumberUsingSchema(
      transportSchema.headers,
      request.headers
    );
    if (!this.schema.validate(request.headers, transportSchema.headers)) {
      this.removePeer(request);
      return next(new Error(`${this.schema.getLastError().details[0].path
      } - ${this.schema.getLastErrors()[0].message}`));
    }
    if (!this.systemModule.networkCompatible(request.headers.nethash as string)) {
      this.removePeer(request);
      // TODO: convert this into an error.
      return next({
        expected: this.systemModule.getNethash(),
        message : 'Request is made on the wrong network',
        received: request.headers.nethash,
      });
    }

    if (!this.systemModule.versionCompatible(request.headers.version)) {
      this.removePeer(request);
      // TODO: Convert this into an error
      return next({
        expected: this.systemModule.getMinVersion(),
        message : 'Request is made from incompatible version',
        received: request.headers.version,
      });
    }

    // Add peer only if not firewalled
    if (typeof request.headers.firewalled === 'undefined' || request.headers.firewalled === 'false') {
      const p = this.peersLogic.create(this.computeBasePeerType(request));
      p.applyHeaders(request.headers as any);
      this.peersModule.update(p);
    }
    next();
  }
开发者ID:RiseVision,项目名称:rise-node,代码行数:38,代码来源:validatePeerHeaders.ts

示例9: verify

  public async verify(tx: IBaseTransaction<MultisigAsset>, sender: AccountsModel): Promise<void> {
    if (!tx.asset || !tx.asset.multisignature) {
      throw new Error('Invalid transaction asset');
    }

    if (!Array.isArray(tx.asset.multisignature.keysgroup)) {
      throw new Error('Invalid multisignature keysgroup. Must be an array');
    }

    if (tx.asset.multisignature.keysgroup.length === 0) {
      throw new Error('Invalid multisignature keysgroup. Must not be empty');
    }

    // check multisig asset is valid hex publickeys
    for (const key of tx.asset.multisignature.keysgroup) {
      if (!key || typeof(key) !== 'string' || key.length !== 64 + 1) {
        throw new Error('Invalid member in keysgroup');
      }
    }

    if (tx.asset.multisignature.min < constants.multisigConstraints.min.minimum ||
      tx.asset.multisignature.min > constants.multisigConstraints.min.maximum) {
      throw new Error(`Invalid multisignature min. Must be between ${constants.multisigConstraints.min.minimum} and ${
        constants.multisigConstraints.min.maximum}`);
    }

    if (tx.asset.multisignature.min > tx.asset.multisignature.keysgroup.length) {
      throw new Error('Invalid multisignature min. Must be less than or equal to keysgroup size');
    }

    if (tx.asset.multisignature.lifetime < constants.multisigConstraints.lifetime.minimum ||
      tx.asset.multisignature.lifetime > constants.multisigConstraints.lifetime.maximum) {
      throw new Error(`Invalid multisignature lifetime. Must be between ${constants.multisigConstraints
        .lifetime.minimum} and ${constants.multisigConstraints.lifetime.maximum}`);
    }

    if (tx.recipientId) {
      throw new Error('Invalid recipient');
    }

    if (tx.amount !== 0) {
      throw new Error('Invalid transaction amount');
    }

    if (this.ready(tx, sender)) {
      for (const key of tx.asset.multisignature.keysgroup) {
        let valid = false;
        if (Array.isArray(tx.signatures)) {
          for (let i = 0; i < tx.signatures.length && !valid; i++) {
            if (key[0] === '+' || key[0] === '-') {
              valid = this.transactionLogic.verifySignature(
                tx,
                Buffer.from(key.substring(1), 'hex'),
                Buffer.from(tx.signatures[i], 'hex'),
                VerificationType.ALL
              );
            }
          }
        }

        if (!valid) {
          throw new Error('Failed to verify signature in multisignature keysgroup');
        }
      }
    }

    if (tx.asset.multisignature.keysgroup.indexOf(`+${sender.publicKey.toString('hex')}`) !== -1) {
      throw new Error('Invalid multisignature keysgroup. Cannot contain sender');
    }

    for (const key of tx.asset.multisignature.keysgroup) {
      if (typeof(key) !== 'string') {
        throw new Error('Invalid member in keysgroup');
      }

      const sign   = key[0];
      const pubKey = key.substring(1);
      if (sign !== '+') {
        throw new Error('Invalid math operator in multisignature keysgroup');
      }

      if (!this.schema.validate(pubKey, { format: 'publicKey' })) {
        throw new Error('Invalid publicKey in multisignature keysgroup');
      }
    }

    // Check for duplicated keys
    if (tx.asset.multisignature.keysgroup.filter((k, i, a) => a.indexOf(k) !== i).length > 0) {
      throw new Error('Encountered duplicate public key in multisignature keysgroup');
    }
  }
开发者ID:RiseVision,项目名称:rise-node,代码行数:91,代码来源:createmultisig.ts


注:本文中的z-schema.validate函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。