JavaScript |基于自定义模式的对象的验证结构

发布于 2025-02-13 08:10:38 字数 721 浏览 2 评论 0原文

处理需要验证传入请求对象的 Strucutre 的API。根据对类似问题的其他答案,我试图创建一个似乎无法正常工作的解决方案。

到目前为止,我的代码:

 const testObj = {
            "uuid": {
                "value": "5481da7-8b7-22db-d326-b6a0a858ae2f",
                "type": "id"
            },
            "rate": {
                "value": 0.12,
                "type": "percent"
            }
        }

        let ok = true;

        ok = testObj === OBJECT_SCHEMA; // is false. Expected to be true

我的验证架构:

const OBJECT_SCHEMA = {
    "uuid": {
        "value": String,
        "type": String
    },
    "rate": {
        "value": Number,
        "type": String
    }
}

Aynone可以指出我在这里做错了什么吗?

Working on a API that needs to validate the strucutre of the incoming request object. Based on other answers to similar questions, I have tried to create a solution that doesn't seem to be working.

My code so far:

 const testObj = {
            "uuid": {
                "value": "5481da7-8b7-22db-d326-b6a0a858ae2f",
                "type": "id"
            },
            "rate": {
                "value": 0.12,
                "type": "percent"
            }
        }

        let ok = true;

        ok = testObj === OBJECT_SCHEMA; // is false. Expected to be true

My validation schema:

const OBJECT_SCHEMA = {
    "uuid": {
        "value": String,
        "type": String
    },
    "rate": {
        "value": Number,
        "type": String
    }
}

Can aynone pinpoint what I'm doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃兔兔 2025-02-20 08:10:38

让您开始的东西:

let validate = (obj, schema) =>
    typeof obj === 'object'
        ? Object.keys(schema).every(k => validate(obj[k], schema[k]))
        : obj?.constructor === schema;

//

const OBJECT_SCHEMA = {
    "uuid": {
        "value": String,
        "type": String
    },
    "rate": {
        "value": Number,
        "type": String
    }
}


testObj = {
    "uuid": {
        "value": "5481da7-8b7-22db-d326-b6a0a858ae2f",
        "type": "id"
    },
    "rate": {
        "value": 0.12,
        "type": "percent"
    }
}

console.log(validate(testObj, OBJECT_SCHEMA))
testObj.rate.value = 'foo'
console.log(validate(testObj, OBJECT_SCHEMA))
delete testObj.rate.value
console.log(validate(testObj, OBJECT_SCHEMA))

Something to get you started:

let validate = (obj, schema) =>
    typeof obj === 'object'
        ? Object.keys(schema).every(k => validate(obj[k], schema[k]))
        : obj?.constructor === schema;

//

const OBJECT_SCHEMA = {
    "uuid": {
        "value": String,
        "type": String
    },
    "rate": {
        "value": Number,
        "type": String
    }
}


testObj = {
    "uuid": {
        "value": "5481da7-8b7-22db-d326-b6a0a858ae2f",
        "type": "id"
    },
    "rate": {
        "value": 0.12,
        "type": "percent"
    }
}

console.log(validate(testObj, OBJECT_SCHEMA))
testObj.rate.value = 'foo'
console.log(validate(testObj, OBJECT_SCHEMA))
delete testObj.rate.value
console.log(validate(testObj, OBJECT_SCHEMA))

嘿咻 2025-02-20 08:10:38

testObj === object_schema是返回false,因为testobj和object_schema类型的值相等,但不是值。

我的建议是结帐 yup or or zod 用于创建验证

testObj === OBJECT_SCHEMA is returning false because the value of testObj and OBJECT_SCHEMA types are equal but not values.

My recommendation would be to checkout Yup or Zod for creating a validation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文