Skip to content

Commit

Permalink
refactor: pattern keyword to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Aug 10, 2020
1 parent ed5972d commit 9436ab2
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yml
Expand Up @@ -21,6 +21,8 @@ overrides:
plugins: ["@typescript-eslint"]
rules:
no-var: 0
"@typescript-eslint/restrict-template-expressions":
[error, allowBoolean: true]
"@typescript-eslint/ban-types": off
"@typescript-eslint/no-empty-interface": off
"@typescript-eslint/no-explicit-any": off
Expand Down
7 changes: 2 additions & 5 deletions lib/compile/rules.js
Expand Up @@ -4,11 +4,8 @@ var ruleModules = require("../dotjs")

module.exports = function rules() {
var RULES = [
{
type: "number",
rules: ["format"],
},
{type: "string", rules: ["pattern", "format"]},
{type: "number", rules: ["format"]},
{type: "string", rules: ["format"]},
{
type: "array",
rules: ["items", "contains", "uniqueItems"],
Expand Down
3 changes: 0 additions & 3 deletions lib/dot/errors.def
Expand Up @@ -101,7 +101,6 @@
'if': "'should match \"' + {{=$ifClause}} + '\" schema'",
not: "'should NOT be valid'",
oneOf: "'should match exactly one schema in oneOf'",
pattern: "'should match pattern \"{{#def.concatSchemaEQ}}\"'",
propertyNames: "'property name \\'{{=$invalidName}}\\' is invalid'",
required: "'{{? it.opts._errorDataPathProperty }}is a required property{{??}}should have required property \\'{{=$missingProperty}}\\'{{?}}'",
type: "'should be {{? $typeIsArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}'",
Expand Down Expand Up @@ -130,7 +129,6 @@
'if': "validate.schema{{=$schemaPath}}",
not: "validate.schema{{=$schemaPath}}",
oneOf: "validate.schema{{=$schemaPath}}",
pattern: "{{#def.schemaRefOrQS}}",
propertyNames: "validate.schema{{=$schemaPath}}",
required: "validate.schema{{=$schemaPath}}",
type: "validate.schema{{=$schemaPath}}",
Expand Down Expand Up @@ -158,7 +156,6 @@
'if': "{ failingKeyword: {{=$ifClause}} }",
not: "{}",
oneOf: "{ passingSchemas: {{=$passingSchemas}} }",
pattern: "{ pattern: {{#def.schemaValueQS}} }",
propertyNames: "{ propertyName: '{{=$invalidName}}' }",
required: "{ missingProperty: '{{=$missingProperty}}' }",
type: "{ type: '{{? $typeIsArray }}{{= $typeSchema.join(\",\") }}{{??}}{{=$typeSchema}}{{?}}' }",
Expand Down
14 changes: 0 additions & 14 deletions lib/dot/pattern.jst

This file was deleted.

1 change: 0 additions & 1 deletion lib/dotjs/index.js
Expand Up @@ -14,7 +14,6 @@ module.exports = {
items: require("./items"),
not: require("./not"),
oneOf: require("./oneOf"),
pattern: require("./pattern"),
properties: require("./properties"),
propertyNames: require("./propertyNames"),
required: require("./required"),
Expand Down
15 changes: 10 additions & 5 deletions lib/keyword.ts
Expand Up @@ -7,6 +7,7 @@ import {
} from "./types"

import {getData, getProperty, toQuotedString} from "./compile/util"
import {quotedString} from "./vocabularies/util"

const IDENTIFIER = /^[a-z_$][a-z0-9_$-]*$/i
const customRuleCode = require("./dotjs/custom")
Expand Down Expand Up @@ -141,7 +142,7 @@ function ruleCode(
$data: $defData,
}: KeywordDefinition = this.definition
if (!code) throw new Error('"code" must be defined')
let schemaCode: string | number
let schemaCode: string | number | boolean
let out = ""
const $data = $defData && it.opts.$data && schema && schema.$data
if ($data) {
Expand All @@ -166,7 +167,9 @@ function ruleCode(
keyword,
data,
$data,
schema,
schemaCode,
usePattern: it.usePattern,
level: it.level,
opts: it.opts,
}
Expand Down Expand Up @@ -218,10 +221,12 @@ function ruleCode(
return out + "}"
}

function schemaRefOrVal(): string | number {
return schemaType === "number" && !$data
? schema
: `validate.schema${it.schemaPath + getProperty(keyword)}`
function schemaRefOrVal(): string | number | boolean {
if (!$data) {
if (schemaType === "number" || schemaType === "boolean") return schema
if (schemaType === "string") return quotedString(schema)
}
return `validate.schema${it.schemaPath + getProperty(keyword)}`
}
}

Expand Down
5 changes: 4 additions & 1 deletion lib/types.ts
Expand Up @@ -115,6 +115,7 @@ export interface CompilationContext {
}
compositeRule: boolean
validate: (schema: object) => boolean
usePattern: (str: string) => string
util: object // TODO
self: object // TODO
}
Expand Down Expand Up @@ -164,10 +165,12 @@ export type Vocabulary = KeywordDefinition[]
export interface KeywordContext {
fail: (condition: string) => void
write: (str: string) => void
usePattern: (str: string) => string
keyword: string
data: string
$data?: string | false
schemaCode: string | number
schema: any
schemaCode: string | number | boolean
// TODO replace level with namespace
level: number
opts: Options
Expand Down
14 changes: 10 additions & 4 deletions lib/vocabularies/util.ts
@@ -1,19 +1,25 @@
export function appendSchema(
schemaCode: string | number,
schemaCode: string | number | boolean,
$data?: string | false
): string {
return $data ? `" + ${schemaCode}` : `${schemaCode}"`
}

export function concatSchema(
schemaCode: string | number,
schemaCode: string | number | boolean,
$data?: string | false
): string | number {
): string | number | boolean {
return $data ? `" + ${schemaCode} + "` : schemaCode
}

export function quotedString(str: string): string {
return JSON.stringify(str)
.replace(/\u2028/g, "\\u2028")
.replace(/\u2029/g, "\\u2029")
}

export function dataNotType(
schemaCode: string | number,
schemaCode: string | number | boolean,
schemaType?: string,
$data?: string | false
): string {
Expand Down
1 change: 1 addition & 0 deletions lib/vocabularies/validation/index.ts
Expand Up @@ -6,6 +6,7 @@ const validation: Vocabulary = [
require("./multipleOf"),
// string
require("./limitLength"),
require("./pattern"),
// object
require("./limitProperties"),
// array
Expand Down
25 changes: 25 additions & 0 deletions lib/vocabularies/validation/pattern.ts
@@ -0,0 +1,25 @@
import {KeywordDefinition} from "../../types"
import {dataNotType} from "../util"

const SCH_TYPE = "string"

const def: KeywordDefinition = {
keyword: "pattern",
type: "string",
schemaType: SCH_TYPE,
$data: true,
code({fail, usePattern, data, $data, schema, schemaCode}) {
const dnt = dataNotType(schemaCode, SCH_TYPE, $data)
const regExp = $data ? `(new RegExp(${schemaCode}))` : usePattern(schema)
fail(dnt + `!${regExp}.test(${data})`)
},
error: {
message: ({$data, schemaCode}) =>
$data
? `'should match pattern "' + ${schemaCode} + '"'`
: `"should match pattern \\"${(<string>schemaCode).slice(1, -1)}\\""`,
params: ({schemaCode}) => `{pattern: ${schemaCode}}`,
},
}

module.exports = def

0 comments on commit 9436ab2

Please sign in to comment.